WHERE子句需要一个附加条款 - 我想

时间:2014-03-04 10:05:19

标签: mysql sql pdo

我的 WHERE 子句假设获取所有状态更新,并检查针对阻止表发布它们的用户。

只要用户未被登录的用户阻止,就会显示状态。

如果要显示状态, WHERE 子句当前要求该块不应处于活动状态,这是正确的。

问题是,目前,如果任何用户对某人有活动阻止,结果将不会显示为活动状态,表示是。

示例:

带3个用户;
Tom,Bill John

正在显示所有状态更新 如果 Tom 阻止帐单,则 Tom 不再能看​​到 Bill 的更新, Bill 看不到 Tom 。 - (这也应该是这样的。)

但是现在因为 Tom Bill 存在活动阻止, John 也无法看到他的更新。

我的WHERE子句不是很正确吗?

//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
        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 :(得分:0)

作为解释......

 SELECT s.id        statID
      , s.from_user statFROM
      , s.status    statSTATUS
      , s.deleted   statDEL
      , s.date      statDATE

      , u.id        usrID
      , u.name      usrNAME
      , u.location  usrLOCATION
      , u.postcode  usrPOSTCODE

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

   FROM tbl_status s

   JOIN tbl_users u
     ON u.id = s.from_user 

   LEFT 
   JOIN tbl_blocking b
     ON b.user = u.id
     OR (   b.active = 0      -- this might not be quite right!!!
        AND b.user != :who
        AND b.blocking != :who
        )

  WHERE s.deleted = 0
    AND b.user IS NULL

  ORDER 
     BY s.date DESC

  LIMIT 200