gpt4 book ai didi

mysql - SQL 多连接整理

转载 作者:行者123 更新时间:2023-11-29 03:04:59 24 4
gpt4 key购买 nike

我有这个声明:

SELECT board.*,numlikes 
FROM board
LEFT JOIN (
SELECT pins.board_id, COUNT(source_user_id) AS numlikes
FROM likes
INNER JOIN pins ON pins.id = likes.pin_id
GROUP BY pins.board_id
) likes ON board.id = likes.board_id
WHERE who_can_tag=''
ORDER BY numlikes DESC
LIMIT 10

然后我可以使用 ".$info['board_name']."board 中提取行。但是,我不擅长多重连接,除了 boardlikes 表之外,我还需要将另一个表连接到此表。第三个表是user,匹配到board.user_iduser.user_id

然后我如何使用此数据从 user 中提取用户名?

不会 ".$info['username']." 仍然在表 board 中搜索字段 username 吗?

最佳答案

这会做你想做的:

SELECT board.*,
`user`.username,
numlikes
FROM board
INNER JOIN `user` ON board.user_id = `user`.user_id
LEFT JOIN (
SELECT pins.board_id, COUNT(source_user_id) AS numlikes
FROM likes
INNER JOIN pins ON pins.id = likes.pin_id
GROUP BY pins.board_id
) likes ON board.id = likes.board_id
WHERE who_can_tag=''
ORDER BY numlikes DESC
LIMIT 10

关于mysql - SQL 多连接整理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17132237/

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