Jump to content

[SOLVED] query using 2 tables


mkybell

Recommended Posts

ok, this is probably pretty simple. I've spent hours and can't get this.

 

I have two tables, one has items being reviewed and the other has the results of the review forms. They're tied together with item IDs used in both. The review table also lists the reviewer. So, basically the item table will list an item with a unique item ID and other fields with item information. A review table record will have the unique item ID, a unique ID for the reviewer, and all the rest of the review info. This table holds all item reviews posted from various reviewers.

 

When a reviewer is logged in I need them to see a list of items that they haven't yet reviewed.

I was very close to getting this using JOIN. I got all items that a particular reviewer hadn't reviewed but also got items that they had if there was a record in the database from someone else having reviewed it.

 

Would someone please help me with this?

Figure I have a table called 'items' and one called 'reviews'.

Both tables have fields labeled 'itemID' and the 'reviews' table has a field called 'reviewerID'.

 

Thanks in advance.

Link to comment
Share on other sites

Nice use of highlighting :)

 

Hmm.. tough situation.  How about

 

SELECT *
FROM items
WHERE itemID NOT IN
  (SELECT itemID FROM reviews WHERE reviewerID = $reviewer)

 

The subquery gets ids for all items reviewed by that reviewer.  Then the outer query gets all items NOT in the list of items reviewed by that reviewer.

Link to comment
Share on other sites

that makes sense but returned all records

 

this is the closest thing that works

 

SELECT *

FROM items

LEFT JOIN reviews

ON items.itemID=reviews.itemID

WHERE review.reviewerID != $reviewer

 

this way almost works but I also get items that have been reviewed but by someone else, so they still exist in the review table

 

if I just change the !=, on the last line, to = then I get items that have already been reviewed by that reviewer. all I need here is to get every other item record but can't figure it out

Link to comment
Share on other sites

mkybell, what do you get from the subquery from my above suggestion?

 

SELECT itemID FROM reviews WHERE reviewerID = $reviewer

 

Does this really fetch all reviews by the current reviewer?

 

If it does, then the query I suggested will work.

Link to comment
Share on other sites

bwochinski,

that variation on the LEFT JOIN worked perfectly

thanks a lot  :)

 

btherl,

the subquery that you listed does work. for some reason the whole query still returns all item records. I don't know  ???

I've read that you can use subqueries to do what you can do with a LEFT JOIN so it must be possible.

Link to comment
Share on other sites

I threw some test data into a couple of tables and this seems to work:

SELECT *

FROM items

LEFT JOIN reviews

ON items.itemID=reviews.itemID

AND reviewerID=$reviewer

WHERE reviewerID IS NULL

 

That's what I was getting at.

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.