0

我有表订单:

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'

但可以添加到这个结果 IDs 结果吗?我想收到 count: 2ids: 2 and 6

4

5 回答 5

4

GROUP_CONCAT可能会有所帮助:

SELECT
    COUNT(Customer) AS CustomerNilsen,
    GROUP_CONCAT(O_Id) as IDS
FROM
    Orders
WHERE
    Customer='Nilsen'
于 2013-04-26T08:46:32.747 回答
1

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

SELECT `O_Id` FROM `Orders`
WHERE `Customer` = 'Nilsen'
于 2013-04-26T08:45:10.760 回答
0

我不太了解您的问题,但据我所知,您只想查询查询中SELECTo_id列:

SELECT COUNT(Customer) AS CustomerNilsen, O_Id AS OrderID FROM Orders WHERE Customer='Nilsen'
于 2013-04-26T08:42:08.543 回答
0

你可以检查这个

SELECT Customer,
         COUNT(Customer) AS CustomerNilsen  
    FROM Orders 
   WHERE Customer = 'Nilsen'
GROUP BY Customer
  HAVING CustomerNilsen  = 1
于 2013-04-26T08:42:20.110 回答
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
于 2013-04-26T08:54:06.167 回答