简体   繁体   English

根据字段值更改mysql表联接

[英]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? 在MySQL中是否可以根据记录的特定字段中的内容来更改表的JOIN?

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. 如果table0中'product'的值= 1,或者如果table0中'product'的值是2,则table2是连接表。

When I try the above example I only get mysql syntax errors. 当我尝试上面的例子时,我只会得到mysql语法错误。 Ive also tried it with CASE statement instead of the IF but still not working. Ive也使用CASE语句而不是IF进行了尝试,但仍然无法正常工作。

I think a subselect will be a better solution than JOIN in this case. 我认为在这种情况下,子选择将是比JOIN更好的解决方案。 You can use a CASE statement with a SELECT inside it. 您可以在CASE语句中使用SELECT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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