gpt4 book ai didi

mysql - mysql 中的表连接返回 null

转载 作者:行者123 更新时间:2023-11-29 12:43:18 24 4
gpt4 key购买 nike

我在 mysql 中有两个表,当我尝试根据公共(public)列连接这两个表时,它返回 null,即使这两个表中的列中的值相同。

Table_1 structure :

id VARCHAR (!2) - index
string1 VARCHAR (15)
string2 VARCHAR (50)
string3 VARCHAR (20)
city VARCHAR (50)
zip VARCHAR (20)


Table_2 structure :


string1 VARCHAR (15) -- index
string2 VARCHAR (50) -- index
string3 VARCHAR (20)
city VARCHAR (50)
zip VARCHAR (20)
id_1 int (6)

我使用了查询:

SELECT p.id, pr.id_1
FROM table_1 as p
LEFT JOIN table_2 as pr
ON p.string1 = pr.string1
AND p.string2 = pr.string2
AND p.city = pr.city
AND p.zip = pr.zip

此查询返回 id_1 的 NULL。在这两个表中,大多数行的列数据都相同,我不明白为什么会发生这种情况。有人可以告诉我为什么会发生这种情况以及为什么会发生这种情况。

数据模型:

表_1

id_1    string1     string2    string3    city    zip
1 abc cde efg ghi 00001-13
2 cde efg abc abc 00002

id_2 string1 string2 string3 city zip
3 abc cde efg ghi 00001-13
4 cde efg abc abc 00002

预期结果:

id_1    id_2
1 3
2 4

得到的结果:

id_1    id_2    
1 null
2 null

最佳答案

SELECT p.id, pr.id_1
FROM table_1 as p
LEFT JOIN table_2 as pr
ON p.string1 = pr.string1
AND p.string1 = pr.string2
AND p.city = pr.city
AND p.zip = pr.zip

我没有发现此查询有任何问题

demo

关于mysql - mysql 中的表连接返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25812473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com