SQL issue with getting correct data.

            SELECT *, DATE_FORMAT(search_request_date, '%Y-%m-%d') as mydate
            FROM video_searches t1
            INNER JOIN video_status t2 ON t1.record_id = t2.rel_record_id
            LEFT JOIN video_ratings t3 ON t2.rel_record_id = t3.rel_record_id
            WHERE 0 = 0
            AND t2.status_id = 1
            AND (t3.user_id IS NULL OR t3.user_id =! '#SESSION.user_id#')
            AND DATE(search_request_date) > DATE_SUB(CURDATE(), INTERVAL 2 DAY)
            GROUP BY mydate, t1.record_id

Open in new window


I seem to be having some issues with this query above... "t1" and "t2" tables are pretty standard id relationship tables (t1.record_id = t2.rel_record_id).  The third table is a middle table I populate for videos where a "discoverer user" role has accepted a video for a "judge user" role to judge. I want to query my data so all pending videos by the discover user are shown to the judges based off their user_id not existing in the t3.user_ratings table or that they've already judged the video equaling their user_id existing then. If judge A logs in and  goes to pending videos they see x amount of videos judging each one and submitting till they are all gone. If judge B goes in they would see the same set of videos and go through the same process.
brihol44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David ToddSenior DBACommented:
Hi,

Can you show some sample data that illustrates your issue? That is, you have a reasonable looking query with reasonable looking joins, but the grouping looks like it might be a bit suspect. What is the userID condition in the where clause supposed to achieve?

HTH
  David
0
brihol44Author Commented:
Sure! Thx for the help.

Table video_ratings ...

record id = 500
user_id = 2
video_rel_record_id 603247
rating columns....

video_status
record_id = 924
rel_record_id = 603247
admin_id = 1
status_id = 1

video_searches
record_id = 603247
video_id = 10001
some non related stat columns...
0
brihol44Author Commented:
The data above is the row I'm pulling the 1 video that won't go away (record_id = 603247).
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

David ToddSenior DBACommented:
Hi,

Does the left join to video_ratings need to be instead an inner join?

I've asked the moderators to add this to the MySQL topic area.

HTH
  David
0
James0628Commented:
I'm wondering if the following line is correct:

AND (t3.user_id IS NULL OR t3.user_id =! '#SESSION.user_id#')

 Assuming that '#SESSION.user_id#' is the current user, that's going to produce a row for every row in t3 (for t2.rel_record_id) that is for a different user.  I think maybe what you want there is to only produce a row if there is no entry in t3 for t2.rel_record_id and the current user, which is different.

 If that's what you want, maybe just change the Join to:

LEFT JOIN video_ratings t3 ON t2.rel_record_id = t3.rel_record_id
AND t3.user_id = '#SESSION.user_id#'

 And then just check for t3.user_id IS NULL in the Where.


 OTOH, I'm a bit confused by the last part of this sentence:

 > I want to query my data so all pending videos by the discover user are shown to the
 > judges based off their user_id not existing in the t3.user_ratings table or that they've
 > already judged the video equaling their user_id existing then.

 Does the last part mean that you _want_ to include a row if that user already has an entry in t3 for that video (rel_record_id)?  If so, why are you checking t3.user_id =! '#SESSION.user_id#' in the Where?  Assuming again that '#SESSION.user_id#' is the current user,  that's going to explicitly exclude any existing entries in t3 for that user (while including any entries for other users).

 James
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brihol44Author Commented:
Sorry for the delay. Thx!
0
James0628Commented:
You're welcome.  Glad I could help.

 James
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.