我有这样的声明:
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
然后,我可以使用board
从".$info['board_name']."
中提取行。
但是,我不熟悉多个连接,除了board
和likes
表之外,我还需要加入另一个表。
第三个表格为user
,并与board.user_id
匹配user.user_id
。
如何使用此数据从user
中提取用户名?
".$info['username']."
不会username
在表board
中搜索字段{{1}}吗?
答案 0 :(得分:1)
这将做你想要的:
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