2

我有这样的声明:

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

然后我可以从boardusing中提取行".$info['board_name']."。但是,我不擅长使用多个联接,除了boardandlikes表之外,我还需要将另一个表加入到该表中。第三个表是user并匹配到board.user_idwith user.user_id

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

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

4

1 回答 1

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
于 2013-06-16T10:27:15.350 回答