0

有两个表:

  • 客户( id, name)
  • Order ( id, id_client, name),其中id_client- 外键。

编写一个查询,选择第一个表的标识符和名称以及第二个表中与之关联的记录数。结果应按姓氏降序排列。

我试过了

SELECT 
   Clients.id, Clients.name, count(id) 
FROM clients 
INNER JOIN Order ON Clients.id = Order.id_client 
GROUP BY 
   Clients.id, Clients.name 
ORDER BY 
   Clients.name DESC

但它不起作用。怎么了?

4

4 回答 4

3
SELECT
 c.ID,
 c.Name,
 COUNT(o.ID)
FROM
 Clients c
LEFT JOIN [Order] o
ON
 o.id_client = c.id
GROUP BY
 c.ID,
 c.Name
ORDER BY
 c.Name DESC
于 2012-10-25T13:09:07.273 回答
0

SELECT Clients.id, Clients.name, count(client.id) FROM clients INNER JOIN Order on Clients.id=Order.id_client GROUP BY Clients.id, Clients.name ORDER BY Clients.name DESC

于 2012-10-25T13:09:10.273 回答
0

更改count(id)

count(Clients.id)或者count(Order.id)

我不知道你需要哪个表count(id)。我希望你明白问题出在哪里。

于 2012-10-25T13:09:39.233 回答
0
SELECT
 c.ID,
 c.Name,
 COUNT(o.ID)
FROM
 Clients c,
 Order o
WHERE o.id_client = c.id
GROUP BY
 c.ID
 c.Name
于 2012-10-25T13:12:45.033 回答