gpt4 book ai didi

mysql - 为什么这个 MySQL 查询返回两行?

转载 作者:行者123 更新时间:2023-11-29 09:10:07 27 4
gpt4 key购买 nike

我的查询返回两行,其中第二行与第一行完全相同。我想知道为什么会发生这种情况?我知道如果我把 LIMIT 1 放在最后它只会返回一行,所以我要求不要建议这个解决方案。这是我的查询

SELECT u.username,u.password,u.registerDate,u.lastVisitDate,u.lastVisitIp,
u.activationString,
u.active,u.block,u.contact_id,c.name,c.email,c.pPhone,c.sPhone,c.rPhone,c.area_id,
a.name as areaName, a.city_id, ct.name as cityName, ct.state_id, s.name as stateName,
s.country_id, cn.name as countryName
FROM users u
LEFT JOIN contacts c
ON (u.contact_id = c.id)
LEFT JOIN areas a
ON (c.area_id = a.id)
LEFT JOIN cities ct
ON (a.city_id = ct.id)
LEFT JOIN states s
ON (ct.state_id = s.id)
LEFT JOIN countries cn
ON (s.country_id = c.id)
WHERE u.id = 1

以上查询从 6 个表中获取数据,其中包括

users s
contacts c
areas a
cities ct
states s
country cn

上面所有6个表都有名为id的主键,外键如下

users.conntact_id
contacts.area_id
areas.city_id
cities.state_id
states.country_id

为什么它返回两行而不是一行?

最佳答案

您加入的其中一个表具有多个元素,因此所有其他表都会重复。该表很可能是 contacts 表。

关于mysql - 为什么这个 MySQL 查询返回两行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5828655/

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