简体   繁体   中英

Change mysql table join based on field value

Is it possible in MySQL to change the JOIN of a table based on whats in a particular field of a record?

Example:

SELECT 
CASE table0.status
WHEN 1 THEN table1.id
WHEN 2 THEN table2.id
END
FROM 
table0, table1, table2
IF (table0.status = 1, INNER JOIN queue ON table1.id = table0.product, INNER JOIN queue ON table2.id = table0.product)

I need the joined table to be table1 if the value for 'product' in table0 = 1 or table2 if the value for 'product' in table0 is 2.

When I try the above example I only get mysql syntax errors. Ive also tried it with CASE statement instead of the IF but still not working.

I think a subselect will be a better solution than JOIN in this case. You can use a CASE statement with a SELECT inside it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM