【问题标题】:Why is this query ignoring the WHERE clause?为什么此查询忽略 WHERE 子句?
【发布时间】:2014-02-22 08:38:09
【问题描述】:

这让我快疯了。

我的查询忽略了 WHERE 子句,只输出所有结果。

我知道它仍在读取脚本,因为当我将脚本更改为荒谬的内容时,例如 WHERE user=xxx is throws back an error。

帮助?

$data = mysql_query(
        "SELECT 

        tbl_messages.id as msgID, 
        tbl_messages.from_user as msgFROM,
        tbl_messages.to_user as msgTO, 
        tbl_messages.the_message as msgMESSAGE, 
        tbl_messages.status as msgSTATUS, 
        tbl_messages.date as msgDATE, 
        tbl_messages.subject as msgSUBJECT,

        tbl_users.id as usrID, 
        tbl_users.name as usrNAME,

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

        FROM tbl_messages 
        LEFT JOIN tbl_users 
        ON tbl_messages.from_user = tbl_users.id 

        LEFT JOIN tbl_photos ON tbl_photos.profile = tbl_users.id 

        WHERE tbl_messages.to_user = 65 AND tbl_photos.default_photo IS NULL OR tbl_photos.default_photo = '1'
        ORDER BY status DESC")

【问题讨论】:

    标签: where


    【解决方案1】:

    这只是一个猜测,但您可能需要在 WHERE 子句的某些部分加上括号;正如所写,它可能对所有行都评估为true

    WHERE tbl_messages.to_user = 65
        AND (tbl_photos.default_photo IS NULL OR tbl_photos.default_photo = '1')
    

    【讨论】:

    • 等等,不——拿着电话。你是对的!我误会你了。
    【解决方案2】:

    我觉得你也需要这样的:

    WHERE tbl_messages.to_user = 65 
    AND tbl_photos.default_photo ='NULL' OR tbl_photos.default_photo = 1
    ORDER BY status DESC
    

    或者像这样:

    WHERE tbl_messages.to_user = 65 
    AND tbl_photos.default_photo ='NULL' OR tbl_photos.default_photo = '1'
    ORDER BY status DESC
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      • 2012-09-18
      • 2012-12-29
      • 2011-12-15
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多