ryy705 Posted February 12, 2009 Share Posted February 12, 2009 Hello, Lets assume that I have the following four tables in my database: Magazine: mag_id (primary key) name (Nature, Time, etc) cat_id (reference to Magazine_Category.cat_id) Magazine_Category: cat_id (primary key) category_name (Weekly, Monthly, etc) User user_id (primary key) user_name Subscription user_id (reference to User.user_id) mag_id (reference to Magazine.mag_id) Now I wish to display each magazine's id, name, category, and how many users subscribe to it. If I were to try the following: SELECT Magazine.mag_id, Magazine.mag_name, Category.name, COUNT(Subscription.user_id) FROM Magazine, Magazine_Catergory, Subscription WHERE Magazine.cat_id= Magazine_Category.cat_id AND Magazine.mag_id=Subscription.mag_id GROUP BY Magazine.mag_name ORDER BY Magazine.mag_name It would give me all magazines that have at least one subscriber. But how can I also include magazines that do not have a subscriber yet? Please help. Kindly ask if something is not clear. Quote Link to comment https://forums.phpfreaks.com/topic/145010-solved-how-to-include-all-magazines/ Share on other sites More sharing options...
xtopolis Posted February 13, 2009 Share Posted February 13, 2009 You will want to use a LEFT join. Perhaps something like this to get you started, (untested): SELECT m.mag_id, m.name, COUNT(s.user_id) FROM Magazine m LEFT JOIN Subscription s USING(mag_id) Try that and see what it comes up with. Quote Link to comment https://forums.phpfreaks.com/topic/145010-solved-how-to-include-all-magazines/#findComment-761034 Share on other sites More sharing options...
ryy705 Posted February 20, 2009 Author Share Posted February 20, 2009 Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/145010-solved-how-to-include-all-magazines/#findComment-767102 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.