Using UNION in a query

** Oops sorry I named the thread the wrong thing on accident, lol **

I’m not sure how to get this working, I might have to do 2 queries.

Does this make sense what I’m trying to do? I don’t know if INNER JOIN is the way to go about this, I am stuck because of the WHERE statement.

SELECT	p.*
FROM	projects AS p

INNER JOIN	project_assigned_users
			AS pu
	ON (
		pu.assign_user = '5'
		AND
		pu.assign_project = p.id
	)

-- This WHERE statement ruins it...
-- I'd like it to say, "OR WHERE"

WHERE		assign_customer = '1'

ORDER BY	current_state ASC

I don’t know. Maybe if you explain what you’re trying to do? :slight_smile:

Oh lol… My bad HAH.

I’m trying to select Projects for a user:
1: if they are inside the project_assigned_user table
2: OR if they are the projects default customer (on the project table)

Use a UNION


SELECT p.*
FROM projects AS p
INNER JOIN  project_assigned_users AS pu
ON pu.assign_project = p.id
WHERE pu.assign_user = '5'
UNION 
SELECT *
FROM projects 
WHERE  assign_customer = '1'
 
ORDER BY current_state ASC

Man that’s a fast reply, sheesh. Thanks, I’m going to look in my SQL book by Mr.Rudy to see how this works :stuck_out_tongue:

check out the difference between UNION and UNION ALL on page 67

:slight_smile:

I guess SELECT DISTINCT would be irrelevant with UNION since there would be two separate selects. This books got a lot of stuff to keep going back to – I think I know something, then I realize I only know a little, haha…

yes, you’re right, SELECT DISTINCT wouldn’t be right in a UNION query

thanks for the kind words about the book

it was a lot of work keeping it short and simple

:slight_smile:

Yeah I can imagine it’d take forever to write. I feel like I’m talking to a celebrity here. LOL