SQL Multiple连接多个where

时间:2013-09-28 18:16:44

标签: mysql sql join

我有三张表是问题。类别,词汇和TEX。我试图弄清楚如何在我的查询中有多个连接,我认为你可以添加任意数量的连接,只要你正确引用它们。

所以,以下两个完全适用于那里:

1

SELECT 
categories.ID AS ID,
categories.ParentID AS ID,
vocabulary.value AS Name
FROM categories
INNER JOIN vocabulary
ON categories.sid=vocabulary.sid
WHERE vocabulary.langid=1

2

SELECT 
categories.ID AS ID,
categories.ParentID AS ID,
tex.value AS Description
FROM categories
INNER JOIN tex
ON categories.tid=tex.tid
WHERE tex.langid=1

但是,如果我尝试将它们组合如下,则不起作用。

categories.ID AS ID,
categories.ParentID AS ID,
vocabulary.value AS Name
tex.value AS Description
FROM categories
INNER JOIN tex
ON categories.tid=tex.tid
WHERE tex.langid=1

INNER JOIN vocabulary
ON categories.sid=vocabulary.sid
WHERE vocabulary.langid=1

有什么想法吗?

提前致谢 约翰

1 个答案:

答案 0 :(得分:1)

MySQL中,当您拥有相同名称的列时,只会显示其中一列。您需要通过提供ALIAS来唯一地识别它们。您可以将条件放在ON子句或WHERE子句上,这可能会产生相同的结果,因为它使用INNER JOIN

SELECT  categories.ID AS CategoryID,
        categories.ParentID AS CategoryParentID,
        vocabulary.value AS Name
        tex.value AS Description
FROM    categories
        INNER JOIN tex
            ON categories.tid = tex.tid
        INNER JOIN vocabulary
            ON categories.sid = vocabulary.sid
WHERE   vocabulary.langid = 1 AND 
        tex.langid = 1