Jump to content

Select all rows from tables joining other table but select only rows that match a condition


AdRock

Recommended Posts

I have 2 queries that I want to join together to make one row

 
This queries returns all rows from both tables which is what i want
SELECT 
	table_A.*,
	table_B.*
FROM table_A
	INNER JOIN 
		table_B ON 
			table_A.code = table_B.code

and this is the output

table_A.id 	|   table_A.code  |   table_B.id  |   table_B.code 	 | 	 table_B.complete
===============================================================================================
1		|      123456     |       1    	  |     123456	 	 |   	yes
2	       	|      654321     |       2   	  |   	654321 		 | 	no      

and this is the second query

SELECT 
	table_C.*,
	table_D.*
FROM table_C
	INNER JOIN 
		table_D ON 
			table_C.code = table_D.code
	INNER JOIN 
		table_B ON 
			table_D.code = table_B.code
WHERE table_B.complete = 'yes'

and again the output

table_C.id  |   table_C.code  |   table_D.id  |   table_D.field2   |    table_B.complete
========================================================================================
1    	    |    123456       |       1       |       123456	   |	     yes

What I've been trying to for the last couple of days is join the 2 queries together to make one query that returns this

table_A.id 	|   table_A.code  |   table_B.id  |   table_B.code 	 | 	 table_B.complete	|   table_C.id  |   table_C.code  |   table_D.id  |   table_D.field2   |    table_B.complete
====================================================================================================================================================================================
1			|	   123456     |       1    	  |     123456	 	 |   	yes				|       1    	|     123456      |   	  1       |       123456	   |		yes
2	       	|      654321     |       2   	  |   	654321 		 | 		no      

All i want it to do is get all rows from tables A and B and return all rows from C and D only if table_B.complete equals "yes".  All tables are joined by the code column which all have the same value

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.