简体   繁体   English

为什么此SQL查询返回零行?

[英]Why does this SQL query return zero rows?

I have three tables, menu_level1 , menu_level2 and menu_level3 . 我有三个表, menu_level1menu_level2menu_level3

The first table has three columns: 第一个表包含三列:

id, url, menuname

I'm hungarian, our language use accented characters, so I have to make menuname column for accented character menus. 我是匈牙利人,我们的语言使用重音符号,因此我必须在重音字符菜单中添加menuname列。

The second and the third tables have these columns: 第二和第三个表具有以下列:

id, url, menuname, relation 

Relation is necessary, because it shows the relationship among the tables. 关系是必要的,因为它显示了表之间的关系。 So, I have a menu system for example: 因此,我有一个菜单系统,例如:

services -> webpage making -> business webpages

the rows looks like below: 这些行如下所示:

menu_level1 menu_level1

url->services

menu_level2 menu_level2

url->webpage making
relation->services

menu_level3 menu_level3

url->business webpages
relation->webpage making

I'd like to select from tables with inner join. 我想从具有内部联接的表中选择。 I use the following code: 我使用以下代码:

SELECT 
    menu_leve2.url AS url2, menu_level1.url AS url1 
FROM 
    menu_level2 
INNER JOIN 
    menu_level1 ON 'menu_level2.relation' = 'menu_level1.url'

But it returns zero rows. 但是它返回零行。 WHY? 为什么? menu_level2 has rows, where the relation is the same like menu_level2's url. menu_level2具有行,其中的关系与menu_level2的url相同。

What is the problem? 问题是什么? Why doesn't work this? 为什么不行呢?

You have enclosed the column names in single quotes, turning them into strings rather than references. 您已将列名称括在单引号中,将其转换为字符串而不是引用。 The two strings are not the same, so the comparison is always false. 这两个字符串不相同,因此比较始终为假。

Try: 尝试:

SELECT menu_level2.url AS url2, menu_level1.url AS url1
FROM menu_level2 INNER JOIN
     menu_level1 
    ON menu_level2.relation = menu_level1.url;

If you are learning SQL, I would advise you to use table aliases: 如果您正在学习SQL,我建议您使用表别名:

SELECT ml2.url AS url2, ml1.url AS url1
FROM menu_level2 ml2 INNER JOIN
     menu_level1 ml1
    ON ml2.relation = ml1.url;

The resulting queries are easier to write and to read. 结果查询更易于编写和阅读。

It seems strange to me that relation would be equated to url . 对我而言, relation等同于url似乎很奇怪。 Without sample data, it is hard to understand whether or not this is correct. 没有样本数据,很难理解这是否正确。 But you should check the join conditions to be sure they are what you want. 但是您应该检查join条件,以确保它们是您想要的。

You are mention the column names in to single quotes. 您在单引号中提到了列名称。

SELECT 
    menu_leve2.url AS url2, menu_level1.url AS url1 
FROM 
    menu_level2 
INNER JOIN 
    menu_level1 ON menu_level2.relation = menu_level1.url

or you can make the alias of the table and call with the reference of alias. 或者,您可以创建表的别名并使用别名的引用进行调用。

SELECT 
    menu_leve2.url AS url2, menu_level1.url AS url1 
FROM 
    menu_level2 ml2,
    menu_level1 ml1
WHERE
    ml2.relation = ml1.url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM