sql从同一个表中添加来自相同的INNER JOIN多个选项

时间:2014-07-11 01:13:40

标签: php mysql sql sql-server

基本上我只想选择和INNER JOIN 2表但是在第二个INNER JOIN上我想添加ON检查它们已连接的属性我想为该JOIN添加3个值。基本上INNER JOIN相同的表两次这是代码:

SELECT * FROM categories 
INNER JOIN user_cat ON user_cat.cat_one = categories.category 
   AND user_cat.cat_two = categories.category 
   AND user_cat.cat_three = categories.category

基本上上面的代码不起作用。当我尝试选择两个表并检查代码是否有效时,两个表都不起作用。这是代码:

SELECT * FROM categories, user_cat 
WHERE user_cat.cat_one = categories.category 
   AND user_cat.cat_two = categories.category 
   AND user_cat.cat_three = categories.category

如何更改我的选择以允许检查所有树列?

1 个答案:

答案 0 :(得分:1)

我认为你正在尝试做类似......

SELECT * 
FROM user_cat UC
LEFT JOIN  categories C1 ON UC.cat_one   = C1.category
LEFT JOIN  categories C2 ON UC.cat_two   = C2.category 
LEFT JOIN  categories C3 ON UC.cat_three = C3.category