我想根据列数据执行不同的SELECT.例如,我有一个表http://sqlfiddle.com/#!2/093a2,我只想在use_schedule = 1时比较start_date和end_date.否则选择所有数据.(一个不同的选择)基本上我只想比较起始和结束日期,如果只有use_schedule是1,如果use_schedule是0然后选择其余的数据.
一个例子可能是这样的
select id, name from table
where use_schedule = 0
else
select id, name, start_date from table
where use_schedule = 0 and current_date >= start_date.
Run Code Online (Sandbox Code Playgroud)
基本上我有数据启用计划,然后查看开始和结束日期.因为如果没有启用计划,则无需查看日期.只需选择数据即可.启用计划后,我希望在选择计划数据时更具选择性.
我试图弄清楚MySQL CASE或IF语句是否可行但不能这样做.我怎么能运行这个选择?
谢谢.
我有一张大桌子,上百万条记录。我必须count(*)为特定条件做一个,但是我无法摆脱它。
count()与InnoDB非常昂贵。我一直在尝试找出MySQL的不同配置,但都是徒劳的。无法加快计数。该应用程序要求结果小于1秒,因为还有其他相关查询要运行。
由于InnoDB的计数方式,任何索引都无济于事。
mysql> EXPLAIN SELECT count(*) FROM `callrequests` WHERE active_call = 1;
+----+-------------+--------------+-------+---------------+-------------+---------+------+---------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------+-------+---------------+-------------+---------+------+---------+--------------------------+
| 1 | SIMPLE | callrequests | index | NULL | active_call | 6 | NULL | 5271135 | Using where; Using index |
+----+-------------+--------------+-------+---------------+-------------+---------+------+---------+--------------------------+
mysql> show index from callrequests;
+--------------+------------+------------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index …Run Code Online (Sandbox Code Playgroud)