来自count查询的ID

时间:2013-04-26 08:39:05

标签: php mysql sql

我有表订单:

O_Id    OrderDate   OrderPrice  Customer
1       2008/11/12  1000        Hansen
2       2008/10/23  1600        Nilsen
3       2008/09/02  700         Hansen
4       2008/09/03  300         Hansen
5       2008/08/30  2000        Jensen
6       2008/10/04  100         Nilsen

和查询:

SELECT COUNT(Customer) AS CustomerNilsen FROM Orders
WHERE Customer='Nilsen'

但是可以添加到此结果ID结果? 我想收到 数:2 ID:2和6

5 个答案:

答案 0 :(得分:4)

GROUP_CONCAT可能会有所帮助:

SELECT
    COUNT(Customer) AS CustomerNilsen,
    GROUP_CONCAT(O_Id) as IDS
FROM
    Orders
WHERE
    Customer='Nilsen'

答案 1 :(得分:1)

最好只获取所有ID,然后使用适当的行计数功能:

SELECT `O_Id` FROM `Orders`
WHERE `Customer` = 'Nilsen'

答案 2 :(得分:0)

我真的不明白你的问题,但是我可以告诉你,只想在查询中SELECTo_id

SELECT COUNT(Customer) AS CustomerNilsen, O_Id AS OrderID FROM Orders WHERE Customer='Nilsen'

答案 3 :(得分:0)

您可以查看

SELECT Customer,
         COUNT(Customer) AS CustomerNilsen  
    FROM Orders 
   WHERE Customer = 'Nilsen'
GROUP BY Customer
  HAVING CustomerNilsen  = 1

答案 4 :(得分:0)

试试这个

SELECT O1.O_Id AS OrderID,Count(O1.O_Id) AS OrderCount FROM Orders  O1
INNER JOIN Orders  O2 ON O1.Customer = O2.Customer 
WHERE O1.Customer='Nilsen'
GROUP BY O1.Customer,O1.O_Id