I have 2 tables set up like this (irrelevant rows omitted):
> product
- id
- user_id
> thread
- id
- prod_id
- user_id
- due_date
now I want to create a query that picks all rows from thread table that either:
- have a thread.user_id of (for example) “5”
- have a prod_id where the product.user_id is “5”
I am assuming there is a way to do this, something like …
SELECT
thread.due_date
FROM
product, thread
WHERE
thread.user_id='5'
OR
// not sure how to finish it
Up to this point, I would just use PHP to do this, but would really like to kick up my SQL knowledge a notch and get this working via SQL if possible.
Thanks –
You should be able to use an outer join to do this:
A problem you might find is that you could get duplicate threads if a thread has a user id 5 but also a product with user id 5. Not sure if that will be an issue in your case though. You may be able to use
SELECT DISTINCTto remove these (thanks Alex Martelli)