MySQL query : all records of one table plus count of another table -


I have 2 tables: User and picture are user key in the image table. So basically each user can have multiple images, and each image belongs to a user. Now, I am trying to create the following questions: I want to select all the user information and the total number of pictures (even if it is 0). How can I do that? Maybe it sounds easy, but I'm trying and unable to find the right query. The only thing I can choose is this information, but only for users who have at least 1 picture, which means that there is at least one record for that key in the image table ... but I I also want to consider users who have no one. Any remedy? Thanks!

You can try the following:

  SELECT u name , IFNULL (sub_pollet, 0) numbers that are included in the user (select the total number (*) total, user_id from the image group by URI_ID) sub-IP ON (sub_p.user_id = U.USUR_ID);  

Test case:

  Create a table user (user_id int, namespace (10)); Create a table image (user_id int); Include user values ​​(1, 'joe'); Insert in user values ​​(2, 'Peter'); Include user values ​​(3, 'bill'); Insert pictures in INSERT VALUES (1); Insert pictures in INSERT VALUES (1); Include the values ​​of photos (2); Include the values ​​of photos (2); Include the values ​​of photos (2);  

Result:

  + ------- + ----- + | Name | Number | + ------- + ----- + | Joe 2 | | Peter | 3 | | Bill | 0 | + ------- + ----- + 3 rows set (0.00 seconds)  

Comments