我的查询有效,但我很难通过合并另一个表来扩展它

时间:2014-02-26 19:06:06

标签: mysql

如此简单,却又如此困难。 我有一个工作正常的查询,但现在我需要进一步扩展它。

目前,查询将输出用户状态更新到网站以及其他信息,例如谁写了状态等。

现在,如果该用户已被阻止(如果它们出现在我的tbl_blocking中),我需要修改代码以不输出用户状态

我完全陷入困境,并且花费了最后这么多时间。

我需要说,
“如果tbl_blocking.user =:谁和tbl_blocking.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,

                tbl_photos.profile as photosPROFILE,
                tbl_photos.photo_link as photoLINK,
                tbl_photos.default_photo as photoDEFAULT

                FROM tbl_status 

                LEFT JOIN tbl_users ON tbl_status.from_user = tbl_users.id
LEFT JOIN tbl_blocking ON tbl_status.from_user = tbl_users.id
                LEFT JOIN tbl_photos ON tbl_photos.profile = tbl_users.id 

                WHERE tbl_status.deleted = '0' AND (tbl_photos.default_photo IS NULL OR tbl_photos.default_photo = '1')
    AND (tbl_blocking.user = :who AND tbl_blocking.blocking !=who) OR IS NULL
                ORDER BY tbl_status.date desc

                    ";

1 个答案:

答案 0 :(得分:0)

实现此目的的一种方法是对ON使用tbl_blocking以下的LEFT JOIN tbl_blocking ON tbl_blocking.blocking = tbl_status.from_user AND tbl_blocking.user = :who 子句(这似乎是开头的):

 WHERE ....
    AND tbl_blocking.id IS NULL

然后包括以下WHERE子句:

{{1}}