mysql中on子句错误中的未知列

时间:2014-07-05 00:12:24

标签: mysql

我有两张桌子:

mysql> select * FROM borrower;
+--------+------------+---------+--------------+
| cardno | name       | address | phone        |
+--------+------------+---------+--------------+
|      1 | A          | nj,usa  | 111-222-333 |
|      2 | B          | NY,USA  | 444-555-666 |
|      3 | C          | nj,usa  | 777-888-999 |
+--------+------------+---------+--------------+
3 rows in set (0.00 sec)

mysql> select * FROM bookloans;
+--------+----------+--------+------------+------------+
| bookid | branchid | cardno | dateout    | duedate    |
+--------+----------+--------+------------+------------+
|      1 |        1 |      1 | 2014-04-01 | 2014-08-01 |
+--------+----------+--------+------------+------------+
1 row in set (0.00 sec)
书卷中的cardno是借款人中的cardno的外键。 ç

现在我正在尝试执行以下SQL查询:

mysql> select name,address from borrower full join bookloans
  on bookloans.cardno = borrower.cardno
  group by cardno having count(bookid)=0;
ERROR 1054 (42S22): Unknown column 'borrower.cardno' in 'on clause'

我不理解错误,因为借用表确实有名为cardno的列。知道我做错了吗?

谢谢, 苏尼

1 个答案:

答案 0 :(得分:3)

MySQL不支持full join语法。因此,此查询中的单词full被解释为表别名,就像您执行了如下查询:

... FROM borrower AS `full`
    JOIN bookloans ...

然后当你引用borrower.cardno时,它很混乱,因为它认为你想使用别名full来引用借用者表。

我知道这是MySQL解析器中一个非常愚蠢的错误。我记录了一个关于它的错误(http://bugs.mysql.com/bug.php?id=69858)。你应该点击那个bug上的“影响我”。


重新评论:

在MySQL中进行完全外连接的常用方法是:

SELECT ...
FROM borrower LEFT OUTER JOIN bookloans USING (cardno)
UNION
SELECT ...
FROM borrower RIGHT OUTER JOIN bookloans USING (cardno)

但是,在您的情况下,我认为您实际上不需要完整的外部联接。你正在使用GROUP BY COUNT(bookid) = 0所以看起来你正在寻找一个左外连接,其中书卷中没有匹配的行:

SELECT name, address FROM borrower
LEFT OUTER JOIN bookloans USING (cardno)
WHERE bookloans.cardno IS NULL;