0

There are two tables:

  • Clients (id, name)
  • Order (id, id_client, name), where id_client - foreign key.

Write a query that selects the identifier and name of the first table and the number of records in the second table, associated with them. The result should be sorted by surname in descending order.

I've tried

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

But it doesn't work. What is wrong?

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

count(id)に変更

count(Clients.id)またcount(Order.id)

どのテーブルから必要かわかりませんcount(id)。問題がどこにあるかを理解していただければ幸いです。

于 2012-10-25T13:09:39.233 に答える
0

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

于 2012-10-25T13:09:10.273 に答える
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 に答える