我最近刚发布了一个关于Mysql的文章,结果发现这是我的一个问题,我忘了添加一个字段。现在,这个查询正在工作:
SELECT e.*, b.item_count FROM inventory e, inventory2 b
WHERE (e.sku_id = b.parent_id AND b.item_count > 0)
AND (e.item_name LIKE '%Cherry%' OR e.item_artist_name LIKE '%Cherry%'
OR e.item_description LIKE '%Cherry%' OR e.item_media LIKE '%Cherry%'
OR e.item_genre LIKE '%Cherry%' OR e.item_tags LIKE '%Cherry%')
这很好,但是我不希望它复制结果,现在我得到了以下结果:
+--------+-----------------------------+------------------+
| sku_id | item_name | item_description |
+--------+-----------------------------+------------------+
| 1 | Cherry Blossoms | Description |
| 1 | Cherry Blossoms Description | Description |
| 46 | Wild Cherry | Description |
| 46 | Wild Cherry | Description |
| 1 | Cherry Blossoms Description | Description |
+--------+-----------------------------+------------------+
但是,我希望得到以下输出:
+--------+-----------------------------+-------------------+
| sku_id | item_name | item_description |
+--------+-----------------------------+-------------------+
| 1 | Cherry Blossoms | Description |
| 46 | Wild Cherry | Description |
+--------+-----------------------------+-------------------+
发布于 2015-06-07 07:08:32
您应该按id进行分组,例如:
...myQuery
GROUP BY e.sku_id
注意:不鼓励隐式联接,您应该使用显式联接(如TABLE1 INNER JOIN TABLE2 ON ...
)。
发布于 2015-07-22 06:20:03
您需要一个聚合函数来使用group函数,所以使用aggregrate函数并使用group子句。
https://stackoverflow.com/questions/30690990
复制相似问题