需要你的帮助来编写SQL查询(!)

时间:2021-07-16 19:23:11

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 id doesn't work ;(

但是id不起作用;(

1 个解决方案

#1


1  

OK, Here is the query:

好的,这是查询:

i have realized that table name can not be same as keyword. before post this answer, i haven't checked this as the OP give the Table name as 'Order'. here is the corrected version of the previous query: thank to " Jan Dvorak" for correcting me.

我意识到表名不能与关键字相同。在发布此答案之前,我没有检查过这个,因为OP将表名称作为'Order'。这是上一个查询的更正版本:感谢“Jan Dvorak”纠正我。

Select clients.id as `identifier`, clients.name as `Name` from clients INNER JOIN orders on clients.id = orders.id_client order by `Name` DESC;

SQLfiddle demo

#1


1  

OK, Here is the query:

好的,这是查询:

i have realized that table name can not be same as keyword. before post this answer, i haven't checked this as the OP give the Table name as 'Order'. here is the corrected version of the previous query: thank to " Jan Dvorak" for correcting me.

我意识到表名不能与关键字相同。在发布此答案之前,我没有检查过这个,因为OP将表名称作为'Order'。这是上一个查询的更正版本:感谢“Jan Dvorak”纠正我。

Select clients.id as `identifier`, clients.name as `Name` from clients INNER JOIN orders on clients.id = orders.id_client order by `Name` DESC;

SQLfiddle demo