我有2个表格“客户”和“门票”

在“票证”中,我具有“ ID_Client”列,该列具有:1,2,3,4,4,2值。

在“客户”中,我有“名称”和“姓氏”列,它们对应于“票证”中的ID。

查询应显示ID = 2和ID = 4的名称和姓氏

我进行了有效但不完全的查询:

SELECT Clients.Name, Clients.Surname
FROM Clients
INNER JOIN Tickets ON Tickets.ID_Client = Tickets.ID_Client
WHERE Tickets.ID_Client = (SELECT ID_Client FROM Tickets
GROUP BY ID_Client ORDER BY Count(*) DESC Limit 1) Limit 1


它返回ID = 2的客户端的名称和姓氏,但是以某种方式错过了ID = 4的客户端。而且我必须在末尾添加限制1,因为如果没有限制,它将两次显示ID = 2的客户端

最佳答案

如果只需要两行,请使用LIMIT 2

SELECT c.Name, c.Surname
FROM Clients c JOIN
     (SELECT t.ID_Client,
      FROM Tickets t
      GROUP BY t.ID_Client
      ORDER BY Count(*) DESC
      LIMIT 2
     ) t
     ON c.ID_Client = c.ID_Client;

关于mysql - 如何根据表2中最频繁的值显示表1中的值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47249462/

10-12 12:28