Forum Moderators: open

Message Too Old, No Replies

Logic Within a SQL (MySQL) Statement

Branch possibilities in SQL?

         

trillianjedi

2:05 pm on Aug 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I need to do a branch type logic statement within a single SQL statement if at all possible.

Essentially what I have is a join across two tables. If a row does not exist in one table, I want to send the result from the other. If a row does exist in that table, I want to send that one.

Does that make sense ? Possible ?

Thanks!

mark_roach

2:13 pm on Aug 3, 2009 (gmt 0)

10+ Year Member



You could perform a union of the query from each of the tables ordered such that the row from the table you favour is listed first and then just fetch the first row.

Something like :

select product, price, '1' as table_num
from table1
union
select product, price, '2' as table_num
from table2
order by table_num asc
limit 0,1

trillianjedi

2:23 pm on Aug 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, brilliant. That sounds like just the trick.

Thanks Mark - will give the UNION statement a go.