如何使用OR,AND子句改进查询?
explain
select fileds
from table_name
where (fldlp>='2012-09-15 13:00:34' and fldlp<='2012-09-16 06:00:02')
or (flexp>='2012-09-15 13:00:34' and flexp<='2012-09-16 06:00:02');
+----+-------------+------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | table_name | ALL | NULL | NULL | NULL | NULL | 2928790 | Using where |
+----+-------------+------------+------+---------------+------+---------+------+---------+-------------+
答案 0 :(得分:4)
您可以使用BETWEEN
子句。
WHERE (fldlp BETWEEN '2012-09-15 13:00:34' AND '2012-09-16 06:00:02')
OR (flexp BETWEEN '2012-09-15 13:00:34' AND '2012-09-16 06:00:02');
答案 1 :(得分:4)
改善提示
在fldlp
和flexp
上述列的dataType应为 dateTime