有两个表:
Clients (id, name)
Order (id, id_client, name), where id_client - foreign key.
编写一个查询,选择第一个表的标识符和名称以及与之关联的第二个表中的记录数。结果应按姓氏降序排序。
我试过
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
但是id不起作用;(
答案 0 :(得分:1)
好的,这是查询:
我意识到表名不能与关键字相同。在发布此答案之前,我没有检查过这个,因为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;