Issue with this particular SQL query construction

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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?





-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux