下面SQL查询出来的结果如下:
SELECT * FROM item_img_spec WHERE item_id = 2 ORDER BY sort ASC, item_img_spec_id DESC

怎么让他 spec_id 相同的只显示一条
变成这样

SELECT * FROM item_img_spec WHERE item_id = 2 GROUP BY spec_id ORDER BY sort ASC, item_img_spec_id DESC
谢谢大家,直接这样就可以啦
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
SELECT distinct spec_id FROM item_img_spec WHERE item_id = 2 ORDER BY sort ASC, item_img_spec_id DESC
对
spec_id去重试试需要做多次的 Group By 才能得到:
如果item_img_spec_id是表的主键,可以这样写,在spec_id相同保留的情况下保留item_img_spec_id最大的记录