SQL查询未知列

时间:2014-01-02 21:17:10

标签: sql

这是我的SQL查询:

SELECT DISTINCT 
   k.*, m.title as city, w.title as state 
FROM 
   konta__kategoria kat, konta k 
INNER JOIN 
   miejscowosci as m ON k.miejscowosc = m.id 
INNER JOIN 
   wojewodztwa as w ON k.wojewodztwo = w.id 
LEFT JOIN 
   kategorie as cat ON cat.id = kat.kategoria 
WHERE 
   kat.kategoria IN (1610,1609,1608,1607,1606,1605,1611,1612,1613,1614,1615,1616,1617,1618,1619,1620,1621,1622,1623,1624,1625,1626,1627,1628,1629,1630,1631,1632,1633,1634,1635,1636,1637,1638,1639,1640,1641,1642,1643,1644,1645,1646,1647,1648,1649,1650,1651,1652,1653,1654,1655,1656,1657,1658,1659,1660,1661,1662,1663,1664,1665,1666,1667,1668,1669,1670,1671,1672,1673,6) 
   AND kat.parent_id = k.id 
   AND k.dsc LIKE "%%" 
   AND deleted = 0 
ORDER BY 
   k.nazwa

我收到错误:

#1054 - Unknown column 'kat.kategoria' in 'on clause'

删除时它正常工作

LEFT JOIN kategorie as cat ON cat.id = kat.kategoria

但是我知道kat.kategoria存在(它在哪里工作),也存在表cat.id,那么问题是什么?

2 个答案:

答案 0 :(得分:5)

这就是用ANSI 92提出的当前JOIN语法编写查询的方法。这里有一篇Link一篇精彩的文章,说明为什么你不应该使用旧方法这样做。

 SELECT DISTINCT 
           k.*, m.title as city, w.title as state 
        FROM 
           konta__kategoria kat
        INNER JOIN konta k 
            ON kat.parent_id = k.id 
        INNER JOIN miejscowosci as m 
            ON k.miejscowosc = m.id 
        INNER JOIN  wojewodztwa as w 
            ON k.wojewodztwo = w.id 
        LEFT JOIN  kategorie as cat 
            ON cat.id = kat.kategoria 
        WHERE 
           kat.kategoria IN (1610,1609,1608,1607,1606,1605,1611,1612,1613,1614,1615,1616,1617,1618,1619,1620,1621,1622,1623,1624,1625,1626,1627,1628,1629,1630,1631,1632,1633,1634,1635,1636,1637,1638,1639,1640,1641,1642,1643,1644,1645,1646,1647,1648,1649,1650,1651,1652,1653,1654,1655,1656,1657,1658,1659,1660,1661,1662,1663,1664,1665,1666,1667,1668,1669,1670,1671,1672,1673,6) 
           AND k.dsc LIKE "%%" 
           AND deleted = 0 
        ORDER BY 
           k.nazwa

答案 1 :(得分:3)

这是因为您正在使用pre-ansi加入。这是正在发生的事情:JOIN列表与列表中的最后一个表“连接”,即konta k。非ANSI连接的konta__kategoria kat部分不在任何ANSI连接的范围内,从而触发您看到的错误。

您可以通过在任何地方使用ANSI连接来修复它,如下所示:

SELECT DISTINCT 
   k.*, m.title as city, w.title as state 
FROM 
   konta__kategoria kat
CROSS JOIN konta k 
INNER JOIN 
   miejscowosci as m ON k.miejscowosc = m.id 
INNER JOIN 
   wojewodztwa as w ON k.wojewodztwo = w.id 
LEFT JOIN 
   kategorie as cat ON cat.id = kat.kategoria 
WHERE 
   kat.kategoria IN (1610,1609,1608,1607,1606,1605,1611,1612,1613,1614,1615,1616,1617,1618,1619,1620,1621,1622,1623,1624,1625,1626,1627,1628,1629,1630,1631,1632,1633,1634,1635,1636,1637,1638,1639,1640,1641,1642,1643,1644,1645,1646,1647,1648,1649,1650,1651,1652,1653,1654,1655,1656,1657,1658,1659,1660,1661,1662,1663,1664,1665,1666,1667,1668,1669,1670,1671,1672,1673,6) 
   AND kat.parent_id = k.id 
   AND k.dsc LIKE "%%" 
   AND deleted = 0 
ORDER BY 
   k.nazwa

现在,kkat别名都在所有联接的范围内,因此您的查询应该有效。

更新:假设idparent_id不可为空,您不需要交叉联接:

SELECT DISTINCT 
   k.*, m.title as city, w.title as state 
FROM 
   konta__kategoria kat
INNER JOIN
    konta k ON kat.parent_id = k.id
INNER JOIN 
   miejscowosci as m ON k.miejscowosc = m.id 
INNER JOIN 
   wojewodztwa as w ON k.wojewodztwo = w.id 
LEFT JOIN 
   kategorie as cat ON cat.id = kat.kategoria 
WHERE 
   kat.kategoria IN (1610,1609,1608,1607,1606,1605,1611,1612,1613,1614,1615,1616,1617,1618,1619,1620,1621,1622,1623,1624,1625,1626,1627,1628,1629,1630,1631,1632,1633,1634,1635,1636,1637,1638,1639,1640,1641,1642,1643,1644,1645,1646,1647,1648,1649,1650,1651,1652,1653,1654,1655,1656,1657,1658,1659,1660,1661,1662,1663,1664,1665,1666,1667,1668,1669,1670,1671,1672,1673,6) 
   AND k.dsc LIKE "%%" 
   AND deleted = 0 
ORDER BY 
   k.nazwa