lundi 27 juillet 2015

MySQL: If a column value is 2 THEN do X ELSE do Y

The database I am working on right now is somewhat messy. I have three potential tables, that I want to join but in some cases it may only be two tables. Let's call these table1, table2 and table3.

table1 has a field called "type". If table1.type is 2, then I only need to join table3. For any other values I want to join table2 and then table3.

How can I achieve this in one single SQL query rather than: 1) having one query to select the type. 2) make a PHP foreach-loop to check the type of the current iteration and 3) perform a new query according to the type value.

Aucun commentaire:

Enregistrer un commentaire