PHP查询语句3表加入

时间:2016-12-09 17:18:10

标签: php mysql

我很困惑一个查询语句。

我有当前的查询:

$user_id = $_SESSION["user_id"];

$stm = $db->prepare("SELECT votes.hasVoted
FROM votes 
INNER JOIN posts
    ON posts.post_id = votes.post_id 
INNER JOIN users
    ON users.users_id = votes.user_id 
    WHERE users.user_id = :user_id;");
$stm->bindParam(':user_id', $user_id);
$stm->execute();
$votes = $stm->fetchAll();

它不起作用。

我有3张桌子。 [投票,帖子,用户]。 我想检索一下'hasVoted'根据登录用户的会话变量从Votes表中获取值。

投票表:

https://i.stack.imgur.com/ZsHZc.png

帖子表:

https://i.stack.imgur.com/piLPu.png

提前致谢。

我需要帮助: 在我的代码中,我想检查登录用户是否已在特定帖子上投票,如果是,则该按钮将变为不可点击。

我有3个连接的表。

Votes Table has:
vote_id [PK]
user_id [FK]
post_id [FK]

Posts Table has:
post_id [PK]
user_id [FK]

Users Table has:
user_id [PK]

我想将它们与Votes互连,以便我可以从投票中访问post_id和user_id

0 个答案:

没有答案
相关问题