开发者

returning results from 2 mysql tables

开发者 https://www.devze.com 2023-02-01 03:46 出处:网络
I am trying to figure out a complex (at least for me!) mysql query and hoped someone here might have some clue\'s for me..

I am trying to figure out a complex (at least for me!) mysql query and hoped someone here might have some clue's for me..

I have 2 tables "Users" and "files".

Users:

id, name, address, etc..

Files: id, user_id, file_name, etc..

I want to select all rows from Users and in the result create a last column 开发者_如何转开发that has a count of all file with where User.id = Files.user_id.

I tried SELECT * from Users UNION(SELECT COUNT Files.user_id WHERE Users.id = Files.user_id) but doesn't work of course..


select u.*, count(f.id)
from users u
left join files f on u.id = f.user_id
group by u.id
0

精彩评论

暂无评论...
验证码 换一张
取 消