仅在用户编号位于阻止表中时才显示结果

时间:2014-03-03 21:21:04

标签: mysql sql pdo

我有一个充满用户状态更新的数据库表。我想输出所有这些结果,前提是发布该状态的用户尚未被阻止。

正在满足 WHERE 子句..但由于某种原因,它还决定仅输出状态更新,其中'user id'存在于其中一个tbl_blocking列中。

显然,如果该用户在任何时候没有出现在阻止表中(从未被阻止或阻止任何人),他们的状态仍应显示。

我怎样才能克服这一点?我哪里错了?

//Get status results
            $sql = "SELECT 

            tbl_status.id as statID, 
            tbl_status.from_user as statFROM, 
            tbl_status.status as statSTATUS, 
            tbl_status.deleted as statDEL, 
            tbl_status.date as statDATE,

            tbl_users.id as usrID, 
            tbl_users.name as usrNAME,
            tbl_users.location as usrLOCATION,
            tbl_users.postcode as usrPOSTCODE,

            tbl_blocking.id as blockID,
            tbl_blocking.user as blockUSER,
            tbl_blocking.blocking as blockBLOCKING,
            tbl_blocking.date as blockDATE,
            tbl_blocking.active as blockACTIVE

            FROM tbl_status 

            INNER JOIN tbl_users ON tbl_status.from_user = tbl_users.id
            INNER JOIN tbl_blocking ON tbl_users.id = tbl_blocking.user

            WHERE 
            tbl_status.deleted = '0'
            AND tbl_blocking.active = '0'
            AND tbl_blocking.user != :who
            AND tbl_blocking.blocking != :who

            ORDER BY 
            tbl_status.date desc

            LIMIT 200

            ";

2 个答案:

答案 0 :(得分:1)

如果要获取连接表中没有匹配项的行,则必须使用LEFT JOIN。然后,如果您希望能够在连接表中选择特定行,则必须包含特定的NULL测试以保留该行。

        $sql = "SELECT 

        tbl_status.id as statID, 
        tbl_status.from_user as statFROM, 
        tbl_status.status as statSTATUS, 
        tbl_status.deleted as statDEL, 
        tbl_status.date as statDATE,

        tbl_users.id as usrID, 
        tbl_users.name as usrNAME,
        tbl_users.location as usrLOCATION,
        tbl_users.postcode as usrPOSTCODE,

        tbl_blocking.id as blockID,
        tbl_blocking.user as blockUSER,
        tbl_blocking.blocking as blockBLOCKING,
        tbl_blocking.date as blockDATE,
        tbl_blocking.active as blockACTIVE

        FROM tbl_status 

        INNER JOIN tbl_users ON tbl_status.from_user = tbl_users.id
        LEFT JOIN tbl_blocking ON tbl_users.id = tbl_blocking.user

        WHERE 
        tbl_status.deleted = '0'
        AND (tbl_blocking.user IS NULL
            OR (tbl_blocking.active = '0'
                AND tbl_blocking.user != :who
                AND tbl_blocking.blocking != :who))

        ORDER BY 
        tbl_status.date desc

        LIMIT 200

        ";

答案 1 :(得分:0)

问题是您要加入阻止表。这意味着只会显示阻止表和users表中的用户。你想要加入BOTH tbl_blocking和tbl_blocking2似乎。我不确定你的架构,为什么你在同一张桌子上加入两次。

例如:

User  join Blocking

1,2,3       2,3

will return ids (assuming select user.id, blocking.id): 
2,2
3,3

This will OMIT user 1 as blocking does not contain id 1.

User  left join blocking

1,2,3          2,3

will return

1, null (this will be null since blocking does not contain an id of 1)
2, 2
3, 3 

This will include id 1 that you are missing.

以下是更好地解释连接的内容: Joins

你的mysql的摘录:

FROM tbl_status 
INNER JOIN tbl_users ON tbl_status.from_user = tbl_users.id
LEFT JOIN tbl_blocking ON tbl_users.id = tbl_blocking.user

由于状态和用户似乎是1对1记录连接,因此应该没问题。阻止用户的左连接是必要的。这将提供用户表的所有记录,无论阻塞表中的内容如何。我已经包含了一个可以进一步解释的资源。如果能帮到你,我也可以在这里开始聊天。