Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/145010-solved-how-to-include-all-magazines/
Share on other sites

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.