Issue with this particular SQL query construction
| From: | Jonathan Chum | Date: | Tue, 18 Feb 2003 15:57:43 +0000 |
| Subject: | Issue with this particular SQL query construction | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-25740@lists.php.net to get a copy of this message | ||
I have two tables.
Table_1
contact_id (INT 5)
Table_2
contact_id (INT 5)
type (enum '1','2')
data (TEXT)
In table_2, there is a possible of two records that can be associated to an
entry in table in the form of the record type can be an either 1 or 2.
There will always be a type 1, but not necessary a type 2 in Table_2.
So I'm constructing a query in which it queries for data on a LEFT JOIN, but
associates itself to Table_2 where if there is a type = 2, it takes
precendence over records matching type = 1.
An example
Table_1
===========
1
2
3
Table_2
===========
1 | 1 | Just data
1 | 2 | More data
2 | 1 | Just data
3 | 1 | Just data
3 | 2 | More data
So my SQL constrution looks like this:
SELECT * FROM Table_1
LEFT JOIN Table_2 ON Table_1.contact_id = Table_2.contact_id.
So it matches everything, however I don't want type 1 records if type 2
exists. However if a type 2 doesn't exist, I want the record type 1.
How would I construct the SQL query to do that?