mySQL-sql查询多连接问题
我的标题也许不是最能说明问题的 所以我会进一步解释我的问题 我有四张桌子:mySQL-sql查询多连接问题,mysql,Mysql,我的标题也许不是最能说明问题的 所以我会进一步解释我的问题 我有四张桌子: Category - idCategory varchar(100) COLLATE utf8_bin NOT NULL - idMerchant int(11) NOT NULL - parentCategory varchar(100) CHARACTER SET utf8 DEFAULT NULL - level int(11) NOT NULL <-- here the data represent the
Category
- idCategory varchar(100) COLLATE utf8_bin NOT NULL
- idMerchant int(11) NOT NULL
- parentCategory varchar(100) CHARACTER SET utf8 DEFAULT NULL
- level int(11) NOT NULL <-- here the data represent the level of the category
- label varchar(100) CHARACTER SET utf8 NOT NULL
- PRIMARY KEY (`idCategory`,`idMerchant`),
Product
- idProduct int(11) NOT NULL AUTO_INCREMENT
- idCategory varchar(100) NOT NULL
- description varchar(100) DEFAULT NULL
- label varchar(100) DEFAULT NULL
- PRIMARY KEY (`idProduct`)
ProductAttribute
- idProduct int(11) NOT NULL
- idAttribute int(11) NOT NULL
- PRIMARY KEY (`idProduct`,`idAttribute`)
Attribute
- idAttribute int(11) NOT NULL
- code varchar(100) DEFAULT NULL
- label varchar(100) DEFAULT NULL
- PRIMARY KEY (`idAttribute`)
我的期望是得到这样一张桌子:
label | level | C_label | level | P_label | A_Label
Boy | 1 | Watch | 2 | Brand | Festina
... | 1 | Watch | 2 | Brand |Pequignet
... | ... | ... | ... | ... | ...
Girl | 1 | Watch | 2 | Brand |Bell & Ross
...
等等
但结果是这样的:
label | level | C_label | level | P_label | A_Label
Boy | 1 | Watch | 2 | Size |Bell & Ross
... | 1 | Watch | 2 | Size | Burton
... | ... | ... | ... | ... |Coca-Cola <-- I have some brand which are not related with the category
Girl | 1 | Watch | 2 | Size | Swiffer <-- Other example
...
label | level | C| label | level | P|u label | A|label
男孩| 1 |手表| 2 |尺码|贝尔和罗斯
... | 1 |手表| 2 |尺码|伯顿
... | ... | ... | ... | ... |Coca-Cola我看不出此查询如何不返回错误,因为我看不到对rowsourceh
的任何引用,除非在以下谓词中:
ON h.parentCode=pc.idCategory
^
在您列出的四个表的列名中,我也没有看到任何名为parentCode
的列。我看不到列的数据类型(任何隐式数据转换?),任何键定义(主键、唯一键、外键)。没有足够的信息来进一步诊断查询问题
有时,报告类型的“错误结果”与表中的值有关。为了诊断这个问题,我们修改查询,将所有主键列和连接谓词中使用的列都包含在选择列表中。这听起来更像是数据问题,而不是查询问题。例如,尽管有列名,但可能您在一个或多个引用表上有复合主键,或表中有意外值。@GordonLinoff是否使用了“in alias?”@fortune。不,那只是个坏习惯。但是如果查询运行,那么在select
子句中用单引号定义的别名将不会影响结果。@GordonLinoff您向我了解了单引号的用法和别名,我不会在下次查询中使用它们。两个表有复合主键,但我不知道它如何帮助我管理数据。数据库引擎是MyISAM,如果我没有错的话,它不能使用外键,这是我比较熟悉的。这就是为什么我认为我们可以找到ProductAttribute,它只由主键组成,用于引用Product和Attribute表。我的错误。我已经纠正了缺失的元素。
ON h.parentCode=pc.idCategory
^