Jump to content

[SOLVED] Rating System SELECT query


w84me

Recommended Posts

Hi all,

 

I have a problem with a query from a rating system i am currently developing for my school's webpage.

 

I have 3 tables

 

user: with (user_id,name,age) fields

 

items: with (item_id,item_name,item_description) fields

 

ratings: with (rating_id,item_id,user_id,rating) fields

 

What i want is to select all the items that have not been rated from the user yet.

 

I've managed to select all the items that has been rated with this query

 

SELECT items.* FROM items INNER JOIN ratings ON items.item_id = ratings.item_id AND ratings.user_id = '$user_id'

Link to comment
Share on other sites

Use the [ code ] tags for highlighting code in the future please.

I think you'll want something like this:

 

SELECT i.*
FROM items i
JOIN ratings r ON r.item_id = i.item_id AND r.user_id != '$user_id'
GROUP BY i.item_id
ORDER BY i.item_id

 

This will SELECT ALL the items that HAVE RATINGS but are not rated by the user_id you have chosen.

 

note: the ORDER BY in the above statement is superfluous...

Link to comment
Share on other sites

Thank you for your answer and sorry for the code highlighting.

 

This code you've written does indeed what you say but i want it to SELECT ALL that HAVE NO RATINGS and that are NOT rated by my the user_id

 

so if i write

 

SELECT i.*
FROM items i
JOIN ratings r ON r.item_id != i.item_id AND r.user_id != 1
GROUP BY i.item_id
ORDER BY i.item_id

 

I changed that r.item_id != i.item_id and

if i write user_id =1 it works fine...but if i write user_id = 2 it doubles the results and if i use 3 it triples them and so on....

Link to comment
Share on other sites

Um, there's no IS NULL there.

 

You will never ger i.item_id null. This is a left join so all records with an item id in items table will show up irrespective of their matching records in ratings table and there where will filter out the records that don't belong to the userid

 

SELECT i.*
FROM items i
left JOIN ratings r ON i.item_id = r.item_id 
where r.user_id!='$user_id'

Link to comment
Share on other sites

Thank you all for your help but there is still problem...

 

This one is not working. It doesn't fetch any result

 

SELECT i.*
FROM items i
left JOIN ratings r ON i.item_id = r.item_id 
where r.user_id != '$user_id'

 

But this one fetches all the items that the user has rated

 

SELECT i.*
FROM items i
left JOIN ratings r ON i.item_id = r.item_id 
where r.user_id = '$user_id'

 

My sanity is in danger..... :'(

Link to comment
Share on other sites

you have a contradiction in your request:

"SELECT ALL that HAVE NO RATINGS and that are NOT rated by my the user_id"

 

You want ALL the items that are NOT rated , and the ones that are NOT rated by user_id, well the 1st part of your clause negates the 2nd part, ALL items that aren't rated, are obviously not rated by your user_id as well, because NO user has rated them, lol, this isn't the same as what I first thought you wanted.

Hence your new query should be:

SELECT i.*
FROM items i
LEFT JOIN ratings r ON r.item_id = i.item_id 
WHERE r.user_id IS NULL
GROUP BY i.item_id

Link to comment
Share on other sites

You want ALL the items that are NOT rated , and the ones that are NOT rated by user_id, well the 1st part of your clause negates the 2nd part, ALL items that aren't rated, are obviously not rated by your user_id as well, because NO user has rated them, lol, this isn't the same as what I first thought you wanted.

Hence your new query should be:

 

You are right  :-[

 

Hence the problem  with that query is that works only for one user.

 

I am going to have many users with different ids.

Link to comment
Share on other sites

YES!!!! AT LAST!!! ;D ;D ;D

 

This works beautifully!!

 

SELECT i.*
FROM items i
LEFT JOIN ratings r ON r.item_id = i.item_id 
AND r.user_id = '$userid'
WHERE r.user_id IS NULL
GROUP BY i.item_id

 

I've added the

AND r.user_id = '$userid'

.

 

Thank you all!!! You are the best!! I've posted to tens of other forums and nobody could help me with that!!

 

I am going to cry of happiness!!!! :'( ;D :'( ;D

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.