为什么我SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name时显示错误
错误1055(42000):goods_id不在GROUP BY 中
而且我有个疑问,若这个代码执行成功,会有goods_name对应多个goods_id,这时为何会只显示一个goods_id?
错误1055(42000):goods_id不在GROUP BY 中
而且我有个疑问,若这个代码执行成功,会有goods_name对应多个goods_id,这时为何会只显示一个goods_id?
2016-02-17
如果你是想实现和老师相同的显示效果可以这样写
SELECT MIN(goods_id),goods_name FROM tdb_goods GROUP BY goods_name HAVING count(goods_name)>=2;
结果显示如下图
不过根据老师的课程我觉得应该这样写
SELECT t1.goods_id,t1.goods_name FROM tdb_goods AS t1 INNER JOIN (SELECT MIN(goods_id),goods_name FROM tdb_goods GROUP BY goods_name HAVING count(goods_name)>=2) AS t2 ON t1.goods_name = t2.goods_name;
显示结果如下图
举报