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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.