为什么我在字段列表中收到此错误 - 未知列'customers.c_id'

时间:2017-02-01 12:04:23

标签: mysql database

我收到此错误 字段列表中的未知列'customers.c_id' 当我在表格上使用FULL JOIN命令时

我有两张桌子

1个客户2个订单

Host: localhost
Database: abcd
    SQL query: SELECT * FROM `customers` LIMIT 0, 30 ; 
Rows: 6

c_id    c_name  city    pincode
1   gagan   ghaziabad   201005
2   raman   delhi       20200
3   manish  punjab      10123
4   vinay   jammu       31201
5   praveen kerala      40001
6   raghav  goa         10200


Host: localhost
Database: abcd
    SQL query: SELECT * FROM `orders` LIMIT 0, 30 ; 
Rows: 4

orderId c_id    orderDate
1102    5   2014-02-05
1109    2   2015-01-15
1012    3   2016-03-10
1117    7   2017-05-25

我的sql命令是: -

    SELECT customers.c_id,orders.orderId,customers.c_name
FROM customers
FULL JOIN orders
ON
customers.c_id=order.c_id;

sql command error

错误: -

  

ERROR 1054< 42S22>:'字段列表'中的未知列'customers.c_id'

2 个答案:

答案 0 :(得分:1)

MySQL不支持full join,甚至不支持语法。所以你的查询被解释为:

SELECT customers.c_id, orders.orderId, customers.c_name
FROM customers `FULL` INNER JOIN
     orders
     ON customers.c_id = orders.c_id;

因此,名为customers的表在查询中具有别名FULLcustomers中没有列。

我应该补充一点,这个查询不需要FULL JOIN。假设表之间有合理的外键关系,LEFT JOIN将获得所有客户,即使没有订单的客户:

SELECT c.c_id, o.orderId, c.c_name
FROM customers c LEFT JOIN
     orders o
     ON c.c_id = o.c_id;

答案 1 :(得分:1)

由于FULL JOIN不支持UNION,您需要使用LEFT JOINRIGHT JOIN SELECT c.c_id, o.orderId, c.c_name FROM customers c LEFT JOIN orders o ON c.c_id = o.c_id UNION ALL SELECT c.c_id, o.orderId, c.c_name FROM customers c RIGHT JOIN orders o ON c.c_id = o.c_id; 查询{/ 1}}。

ID        int
CreateOn  smalldatetime
FileUrl  nvarchar(max)
FilePath nvarchar(300)
StoreTypeID int