Home >Backend Development >PHP Tutorial >查询商品图片

查询商品图片

WBOY
WBOYOriginal
2016-06-06 20:29:261251browse

有一张商品表:

<code>+--------------+---------------+------+-----+---------+----------------+
| Field        | Type          | Null | Key | Default | Extra          |
+--------------+---------------+------+-----+---------+----------------+
| id           | int(11)       | NO   | PRI | NULL    | auto_increment |
| cate_id      | int(11)       | NO   |     | NULL    |                |
| name         | varchar(100)  | NO   |     | NULL    |                |
| name_extend  | varchar(200)  | NO   |     | NULL    |                |
| price        | decimal(10,2) | NO   |     | NULL    |                |
| origin_price | decimal(10,2) | NO   |     | NULL    |                |
| stock        | int(11)       | NO   |     | NULL    |                |
| desc         | tinytext      | NO   |     | NULL    |                |
| detail       | text          | NO   |     | NULL    |                |
| express      | int(11)       | NO   |     | NULL    |                |
| status       | tinyint(1)    | NO   |     | NULL    |                |
| slide        | tinyint(1)    | NO   |     | NULL    |                |
| is_delete    | tinyint(1)    | NO   |     | NULL    |                |
| add_time     | int(11)       | NO   |     | NULL    |                |
+--------------+---------------+------+-----+---------+----------------+</code>

有一张商品的图片表:

<code>+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| goods_id | int(11)      | NO   |     | NULL    |                |
| img_path | varchar(255) | NO   |     | NULL    |                |
| is_main  | tinyint(1)   | NO   |     | NULL    |                |
| add_time | int(11)      | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+</code>

商品表与商品图片表是一对多的关系,现在要获取商品的信息及所有图片,怎么查会比较合适点?

回复内容:

有一张商品表:

<code>+--------------+---------------+------+-----+---------+----------------+
| Field        | Type          | Null | Key | Default | Extra          |
+--------------+---------------+------+-----+---------+----------------+
| id           | int(11)       | NO   | PRI | NULL    | auto_increment |
| cate_id      | int(11)       | NO   |     | NULL    |                |
| name         | varchar(100)  | NO   |     | NULL    |                |
| name_extend  | varchar(200)  | NO   |     | NULL    |                |
| price        | decimal(10,2) | NO   |     | NULL    |                |
| origin_price | decimal(10,2) | NO   |     | NULL    |                |
| stock        | int(11)       | NO   |     | NULL    |                |
| desc         | tinytext      | NO   |     | NULL    |                |
| detail       | text          | NO   |     | NULL    |                |
| express      | int(11)       | NO   |     | NULL    |                |
| status       | tinyint(1)    | NO   |     | NULL    |                |
| slide        | tinyint(1)    | NO   |     | NULL    |                |
| is_delete    | tinyint(1)    | NO   |     | NULL    |                |
| add_time     | int(11)       | NO   |     | NULL    |                |
+--------------+---------------+------+-----+---------+----------------+</code>

有一张商品的图片表:

<code>+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| goods_id | int(11)      | NO   |     | NULL    |                |
| img_path | varchar(255) | NO   |     | NULL    |                |
| is_main  | tinyint(1)   | NO   |     | NULL    |                |
| add_time | int(11)      | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+</code>

商品表与商品图片表是一对多的关系,现在要获取商品的信息及所有图片,怎么查会比较合适点?

如果商品表是 goods,图片表是 images,需要获取图片的 id 为参数,则:

<code class="sql">SELECT `goods`.`id`, `goods`.`name`, `goods`.`name_extend`, `images`.`img_path` FROM `goods`, `images` WHERE `goods`.`id` = `images`.`goods_id` AND `goods`.`id` = 'ID_IMPUTED';</code>

注意,其中 ID_IMPUTED 为你要指定的商品 id 值。

  1. 建议取消图片表
    如果商品图片的属性(例如:创建时间)不重要的化建议取消图片表.
    如果图片的信息不需要检索的话, 直接插入到exif中). 尽量减少数据冗余.

  2. 建议将图片id插入到商品表中的一列
    建议将图片id插入到商品表中的一列. 图片id即文件名, path根据用户id等其他因素确定.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn