表名是Sample
id name status date
1 ddd 1 2015-03-11
2 dddd 1 2015-03-12
3 dfdfgfg 0 2015-03-11
4 ererre 1 2015-03-19
5 eeeerer 0 2015-03-03
这是表格, - 这是我要添加的查询以获得结果。我希望表中的结果如果状态设置为1则日期不应该过期,如果状态为零则不需要条件。所以它应该显示。所以我做了这样的查询
select * from sample where status='1' AND date>Now() OR status='0'
我得到的结果是正确的
3 dfdfgfg 0 2015-03-11
4 ererre 1 2015-03-19
5 eeeerer 0 2015-03-03
但我想添加更多查询,我如何为上述查询添加更多条件?
答案 0 :(得分:1)
您必须将旧条件和新条件放在括号中,然后使用AND来链接它们:
select * from sample
where (status='1' AND date>Now() OR status='0')
AND (new condition 1)
AND (new condition 2) ...
答案 1 :(得分:0)
SELECT * FROM TABLE_NAME WHERE expiry_date > NOW()
答案 2 :(得分:-1)
尝试使用Union / Union All,只要满足您的要求
包含数据示例的示例代码
create table sample(id INT,name CHAR(10), status INT, date_e DATE);
INSERT INTO sample VALUES(1, 'ddd',1,'2015-03-11');
INSERT INTO sample VALUES(2, 'dddd',1,'2015-03-12');
INSERT INTO sample VALUES(3, 'dfdfgfg',0,'2015-03-11');
INSERT INTO sample VALUES(4, 'ererre',1,'2015-03-19');
INSERT INTO sample VALUES(5, 'eeeerer',0,'2015-03-03');
SELECT * FROM sample;
+------+---------+--------+------------+
| id | name | status | date_e |
+------+---------+--------+------------+
| 1 | ddd | 1 | 2015-03-11 |
| 2 | dddd | 1 | 2015-03-12 |
| 3 | dfdfgfg | 0 | 2015-03-11 |
| 4 | ererre | 1 | 2015-03-19 |
| 5 | eeeerer | 0 | 2015-03-03 |
+------+---------+--------+------------+
5 rows in set (0.00 sec)
mysql> select * from sample where status='1' AND date_e>Now()
-> UNION ALL
-> select * from sample where status='0';
+------+---------+--------+------------+
| id | name | status | date_e |
+------+---------+--------+------------+
| 4 | ererre | 1 | 2015-03-19 |
| 3 | dfdfgfg | 0 | 2015-03-11 |
| 5 | eeeerer | 0 | 2015-03-03 |
+------+---------+--------+------------+
3 rows in set (0.00 sec)