如何使用OR,AND子句改进查询?

时间:2012-09-18 10:35:45

标签: mysql

如何使用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 | 
+----+-------------+------------+------+---------------+------+---------+------+---------+-------------+

2 个答案:

答案 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)

改善提示

  • fldlpflexp

  • 上创建索引
  • 上述列的dataType应为 dateTime