简体   繁体   English

如何使用MySQL Group选择多个记录?

[英]How to select more than one record using MySQL Group by?

I can't seem to use distinct or group by to group certain mySQL records together. 我似乎无法使用distinctgroup by将某些mySQL记录组合在一起。 This SQL query below just returns one line each - I want all the values in the CID though.. 下面这个SQL查询只返回一行 - 我想要CID中的所有值但是...

I want to select a random group by cid or the first set in the table.. I cant use AND CID=X .. is there a way to do it without .. 我想通过cid或表中的第一组选择一个随机组..我不能使用AND CID = X ..有没有办法做到没有..

Table

id      pid     image                           sort_order      cid
-----
2474    50      data/low.jpg            2                       56
2473    50      data/hi.jpg             3                       59
2471    50      data/thumn.jpg      500                     59
2472    50      data/front.jpg      1000                    56
2470    50      data/back.jpg           1                       56

Query 询问

SELECT *
FROM `ocm1__product_image`
WHERE `product_id` = '50'
GROUP BY `cid`
ORDER BY `ocm1__product_image`.`sort_order` ASC
LIMIT 0 , 30

This should return 这应该回来了

id      pid     image                           sort_order      cid
2474    50      data/low.jpg            2                       56
2472    50      data/front.jpg      1000                    56
2470    50      data/back.jpg           1                       56

But it returns both colours.. can I not unique the group? 但它返回两种颜色..我不能独特的组?

It returns this which is wrong, I want to list all cid unique values 它返回错误,我想列出所有cid唯一值

id      pid     image                           sort_order      cid
2474    50      data/low.jpg            2                       56
2471    50      data/thumn.jpg      500                     59

This will return all the entries with the lowest cid for the requested pid . 这将返回所请求的pid具有最低cid所有条目。
It gives the same result as you say that you need, without giving a specific cid as a condition; 它给出了与您所说的相同的结果,而没有给出特定的cid作为条件;

SELECT o1.*
FROM `ocm1__product_image` o1
LEFT JOIN `ocm1__product_image` o2
  ON o1.pid=o2.pid AND o1.cid > o2.cid
WHERE o1.`pid` = '50' AND o2.cid IS NULL
ORDER BY `o1`.`sort_order` ASC
LIMIT 0 , 30

An SQLfiddle to test with . 一个要测试的SQLfiddle

Don't use group by to sort them, you just need to use order by in addition you must also sort them using the CID value for specific Customer ID only, You have the value Product ID (PID) but you didn't provide the CID value into your samples. 不要使用group by对它们进行排序,你只需要使用order by另外你还必须使用特定Customer IDCID值对它们进行排序,你有值Product ID (PID)但是你没有提供CID值进入您的样本。

SELECT *
FROM `ocm1__product_image`
WHERE `product_id` = '50' and `cid` = '56'
ORDER BY `ocm1__product_image`.`sort_order` ASC
LIMIT 0 , 30

or if you want to sort them using the ID , to know where is the first data inserted by using the primary key 或者如果要使用ID对它们进行排序,以了解使用primary key插入的第一个数据的位置

SELECT *
FROM `ocm1__product_image`
WHERE `product_id` = '50' and `cid` = '56'
ORDER BY `ocm1__product_image`.`id` ASC
LIMIT 0 , 30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM