[英]Trying to join multiple tables in mysql - get error 1054, Unknown Column
I want to join multiple tables using JOINs, and mysql/mariadb is refusing to find one column.我想使用 JOIN 连接多个表,而 mysql/mariadb 拒绝找到一列。 The column exists, of course, and I can't figure out what the cause might be.
当然,该专栏存在,我无法弄清楚原因可能是什么。
Table Layout表格布局
CREATE TABLE `shops` (
`id` int(11) NOT NULL,
`name` varchar(32) NOT NULL,
);
CREATE TABLE `shops2categories` (
`shopid` int(11) NOT NULL,
`categoryid` int(11) NOT NULL,
);
CREATE TABLE `categories` (
`id` int(11) NOT NULL,
`name` varchar(64) NOT NULL,
);
CREATE TABLE `categories2items` (
`itemid` int(11) NOT NULL,
`categoryid` int(11) NOT NULL
);
CREATE TABLE `items` (
`id` int(11) NOT NULL,
`name` varchar(32) NOT NULL,
);
Query: In order to avoid confusion with aliases, I now ran the query with the original table names.查询:为了避免与别名混淆,我现在使用原始表名运行查询。
SELECT
shops.name,
categories.name,
items.name
FROM shops
LEFT JOIN shops2categories ON shops2categories.shopid = shops.id
LEFT JOIN categories2items ON categories2items.categoryid = categories.id
LEFT JOIN categories ON categories.id = shops2categories.categoryid
LEFT JOIN items ON items.id = categories2items.itemid
Error Message:错误信息:
#1054 - Unknown column 'categories.id' in 'on clause'
No matter how I restructured my query (foreign key first, primary key first, items table first, categories table first, ..., using different JOIN types), I can't seem to get this to work.无论我如何重组我的查询(外键优先,主键优先,项目表优先,类别表优先,......,使用不同的 JOIN 类型),我似乎都无法让它工作。 Also I read through a whole lot of SO questions to this topic, but I feel that the order is correct, I am not inserting nor updating, and it's not about quoting.
我也通读了很多关于这个主题的 SO 问题,但我觉得顺序是正确的,我不是插入也不是更新,也不是引用。 Something is fundamentally broken in my concept, and I'm very keen on learning what this could be.
我的概念从根本上被打破了,我非常渴望了解这可能是什么。
Look at your from
clause.看看你的
from
子句。 You reference c.id
before you have defined c
.您在定义
c
之前引用c.id
。
A table cannot be referenced until it is defined in the FROM
clause.在
FROM
子句中定义表之前,不能引用它。
You would seem to want:你似乎想要:
FROM shops s LEFT JOIN
shops2categories s2c
ON s2c.shopid = s.id LEFT JOIN
categories c
ON c.id = s2c.categoryid LEFT JOIN
categories2items c2i
ON c2i.categoryid = c.id LEFT JOIN
items i
ON i.id = c2i.itemid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.