Tutorial :Can I do this with MySQL or do I need to get PHP involved?


I have two MySQL tables with a similar structure. i.e.

Table_A - Col1, Col2, Col3, Col4, Col5    Table_B - Col1, Col2, Col3  

I need to run a query that says something like:

SELECT * FROM Table_A WHERE (Table_A.Col1 DOES NOT EXISTS in Table_B.Col3 AND Table_B.Col2 = 'A') AND Table_A.Col5 = 2 etc...  

So basically I need to select everything from A that isn't in B where B has extra conditions.

Hopefully the following example will help:

Table_A example rows

1, 5, 'Orange', 'Elephant', 1  2, 3, 'Red', 'Horse', 2  3, 4, 'Blue', 'Rhino', 2  4, 4, 'Pink', 'Cat', 2  5, 2, 'Purple', 'Dog', 2  

Table_B example rows

1, 'A', 3  2, 'A', 2  3, 'B', 2  4, 'C', 3  

The returned data I need is - I'm querying for all rows from Table_A that (aren't in Table_B AND Table_B.Col2 = 'A') AND Table_A.col5 = 2

4, 4, 'Pink', 'Cat', 2  5, 2, 'Purple', 'Dog', 2  

Any ideas? Thanks!


SELECT A.* FROM table_A AS A  LEFT JOIN Table_B AS B ON A.Col1 = B.Col3  WHERE B.Col3 IS NULL AND B.Col2 = 'A' AND ...  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »