我有两张桌子。
USER: ID | NAME_USER
CAR: ID | ID_USER | NAME_CAR
我想要有最大车的打印用户,我该怎么办?
我的尝试(不工作):
SELECT `NAME_USER`, NAME_CARFROM FROM USER, CAR
感谢您的帮助
答案 0 :(得分:0)
应该是这个
select user.name_user
from user
inner join (select id_user, count(*) num
from car group by id_user order by num desc limit 1) as t on t.id_user = user.id
答案 1 :(得分:0)
加入表格,计算每位用户的车辆数量,并显示数量最多的车辆。
SELECT name_user
FROM user
JOIN car
GROUP BY id_user
ORDER BY COUNT(*) DESC
LIMIT 1
如果多个用户拥有相同的最大汽车数量,则会随意选择其中一个。