我有这样的查询:

    SELECT old.NPP,
       old.NAMA,
       old.JOB,
       pgh.status
    FROM a old
    LEFT JOIN b pgh ON old.person_id = pgh.person_id
                    AND pgh.periode = 2015
                    AND pgh.LAST_STATUS = 1
    WHERE old.UNIT_BESARAN = 'DIVISI TEKNOLOGI INFORMASI'
    AND pgh.status = #status
    ORDER BY pgh.status


我希望我的查询可以从pgh状态获取每个值,例如当我键入status 5时,查询必须返回status为5,但是当我输入null时,状态为null时查询将从数据库返回数据。

如何设置查询?

最佳答案

您可以这样写条件:

where old.UNIT_BESARAN = 'DIVISI TEKNOLOGI INFORMASI' and
      (pgh.status = #status or pgh.status is null and #status is null)

关于mysql - 选择空还是不空取决于参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33602405/

10-16 14:45