Jump to content

Best method of pulling data.


onlyican

Recommended Posts

I am re-building one of my old sites, adding a few extra features.

 

Basically I have a list of stories and comments

I want to pull latest stories and the comments for them stories if any and where valid

 

Table Strus

tblStories

stories_id INT(11)

stories_owner INT(11)

stories_status TINYINT(2)

story_title VARCHAR(255)

 

tblUsers

users_id INT(11)

username VARCHAR(255)

user_level INT(11)

 

tblComments

comments_id INT(11)

comments_story_id INT(11)

comment_owner_id INT(11)

comment_text VARCHAR(255)

comment_status TINYINT(2)

 

** This is a basic stru and not correct

 

SO I have the basic query

** Using ALL as cant be assed to write every field here, but yes I would in real sitution

 

SELECT * FROM tblStories

INNER JOIN tblUsers ON tblStories.stories_owner = tblUsers.users_id

LEFT JOIN tblComments ON tblStories.stories_id = tblComments.comments_story_id

INNER JOIN tblUsers AS CommentUser ON CommentUser.users_id = tblComments.comment_owner_id

 

THIS works fine

 

But I have flags in each table, the *_status says if its pending / online ect (2 is online)

So a story / comment can be hidden / deleted

 

 

If I add the following

 

WHERE tblStories.stories_status = 2

AND tblUsers.users_level >=5

AND tblComments.comments_status = 2

AND CommentUser.users_level >= 5

 

Any story without comments does not show.

 

So basically I want to pull all stories that are live, (status = 2)

With all users who are valid users (user_level >= 5)

 

And all comments on the same. (status = 2 and user_level for the person who added comment >= 5)

 

I am doing the user, so if a user is removed, it will automatically remove all comments.

 

As mentioned, adding the WHERES does not bring up stories where there are no comments, as the comment_status is NOT greater than 2

 

Link to comment
Share on other sites

try this (no tested):

 

SELECT * FROM tblStories
   INNER JOIN tblUsers ON tblStories.stories_owner = tblUsers.users_id AND tblUsers.users_level >= 5
   LEFT JOIN tblsComments ON tblUsers.users_id = tblComments.comment_owner_id  AND tblComments.comments_status = 2 AND  tblStories.stories_id = tblComments.comments_story_id
WHERE tblStories.stories_status = 2

 

Link to comment
Share on other sites

For the comments table join since it is a LEFT JOIN, if there are no matching results then it will return NULL.  Because of that you should be adding your condition for the status on that table = in the ON clause instead of in there WHERE clause.  Because if it appears in the WHERE clause it needs to find a comment to show up (you could just have the condition as: tblComments.comments_status = 2 OR tblComments.comments_status IS NULL).

 

~juddster

Link to comment
Share on other sites

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.