验证where子句中的列

时间:2013-09-13 19:13:18

标签: php mysql sql

我需要验证2列,但有时第二列是空的 然后我需要验证第二列,如果它不是空的,否则它将只验证第一列

SELECT * 
FROM table 
WHERE column1 = $_GET['id'] 
and column2 IS NOT NULL = $_GET['id']

2 个答案:

答案 0 :(得分:3)

这是你想要的逻辑吗?

SELECT *
FROM table
WHERE column1 = $_GET['id'] and
      (column2 IS NULL or column2 = $_GET['id']);

答案 1 :(得分:0)

SELECT IF(column1 IS NULL, column2, column1)
FROM table 
WHERE column1 = $_GET['id'] OR column2 = $_GET['id']