MySQL JOIN 和 ORDER 问题(对多个表进行排序)

Posted

技术标签:

【中文标题】MySQL JOIN 和 ORDER 问题(对多个表进行排序)【英文标题】:MySQL JOIN and ORDER problem (sorting over multiple tables) 【发布时间】:2011-08-24 19:57:05 【问题描述】:

我有一个大问题。我需要加入三个表并通过日期列对数据进行排序。 我已经尝试过 Sub-Selects、Union 和 Joins - 没有成功。

portal_gm_provide 是存储全局 wms_id 的表。 portal_gm_log 是存储所有日志记录信息(信息、警告、错误)的表。 portal_gm_cron 是存储 cronjob 信息的表(成功或错误)。

问题在于这三个表的结构不同,日期字段也不同。 我可以像左/右连接一样加入和排序每个适合的数据都在一行中并且不需要的字段为 NULL 的数据吗?


选择 db_main.wms_id AS id_wms, db_log.message、db_log.message_real、db_log.id_layer、 db_log.code、db_log.subcode、db_log.counter、db_log.time_first、db_log.time_last、 db_cron.error, db_cron.errorno, 案子 当 db_log.time_first 为 NULL 时,则 db_cron.time ELSE NULL # db_log.time_first 结束时间 FROM portal_gm_provide AS db_main 右连接 portal_gm_log AS db_log ON db_log.id_wms = db_main.wms_id 右连接 portal_gm_cron AS db_cron ON db_cron.id_wms = db_main.wms_id 哪里 db_main.user_id = 1 按时间顺序排列 限制 50
如果不存在“portal_gm_provide”,则创建表( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(10) unsigned NOT NULL DEFAULT '0', `wms_id` int(10) unsigned NOT NULL DEFAULT '0', `mail_notify` varchar(100) 默认为空, `mail_period` varchar(100) 默认为空, `time` datetime NOT NULL, `time_cron` 日期时间默认为 NULL, 主键(`id`) ) 引擎=MyISAM 默认字符集=utf8; 如果不存在“portal_gm_log”,则创建表( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `code` int(10) unsigned NOT NULL DEFAULT '0', `subcode` int(10) unsigned DEFAULT '0', `id_wms` int(10) unsigned DEFAULT '0', `id_layer` int(10) unsigned DEFAULT '0', `message` 中文本, `message_real` 中文本, `counter` int(10) unsigned NOT NULL DEFAULT '1', `time_first` 日期时间不为空, `time_last` 日期时间默认为 NULL, 主键(`id`), KEY `id_wms` (`id_wms`), KEY `id_layer` (`id_layer`) ) 引擎=MyISAM 默认字符集=utf8; 如果不存在“portal_gm_cron”,则创建表( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `id_wms` int(10) unsigned DEFAULT '0', `error` varchar(250) 默认为空, `errorno` int(10) 无符号默认 '0', `flag` int(10) 无符号默认 '0', `time` 日期时间 默认为 NULL, 主键(`id`), KEY `GM_ID` (`id_wms`), KEY `DATUM`(`时间`) ) 引擎=InnoDB 默认字符集=utf8; 插入`portal_gm_provide`(`id`、`user_id`、`wms_id`、`mail_notify`、`mail_period`、`time`、`time_cron`)值 (1, 1, 2, 'MOD_GEOMONITOR_PROVIDE_NOTIFY_STATE_WARNINGERROR', 'MOD_GEOMONITOR_PROVIDE_NOTIFY_PERIOD_3H', '2011-04-21 10:35:36', '2011-05-13 10:10:02'), (2、1、1389、'MOD_GEOMONITOR_PROVIDE_NOTIFY_STATE_WARNINGERRORIN'、'MOD_GEOMONITOR_PROVIDE_NOTIFY_PERIOD_IMMEDIATELY'、'2011-05-02 13:42:02'、'2011-05-13 11:34:01'), (3、1、415、'MOD_GEOMONITOR_PROVIDE_NOTIFY_STATE_WARNINGERRORIN'、'MOD_GEOMONITOR_PROVIDE_NOTIFY_PERIOD_IMMEDIATELY'、'2011-05-02 13:42:02'、'2011-05-13 11:53:01'); 插入`portal_gm_log`(`id`,`code`,`subcode`,`id_wms`,`id_layer`,`message`,`message_real`,`counter`,`time_first`,`time_last`)值(578, 8192, 0, 415, 5975, '未找到有效的 SRS。可能没有匹配的边界框。默认为 EPSG:4326。', NULL, 90, '2010-10-21 20:19:42', '2010-10-27 13:42:52'); 插入`portal_gm_log`(`id`、`code`、`subcode`、`id_wms`、`id_layer`、`message`、`message_real`、`counter`、`time_first`、`time_last`)值(3984, 8192, 0, 415, 5975, '没有找到给定 SRS 的有效框。可能没有匹配的边界框。默认为 EPSG:4326。', NULL, 483, '2010-10-27 15:16:05', '2010 -12-02 08:18:19'); 插入`portal_gm_log`(`id`,`code`,`subcode`,`id_wms`,`id_layer`,`message`,`message_real`,`counter`,`time_first`,`time_last`)值(7800, 4352, 0, 415, 0, 'www.umweltkarten.mv-regierung.de', '提供的每个名称服务器都失败:成功', 3, '2010-11-26 00:37:05', '2010-11 -29 21:34:27'); 插入`portal_gm_log`(`id`、`code`、`subcode`、`id_wms`、`id_layer`、`message`、`message_real`、`counter`、`time_first`、`time_last`)值(14471, 12288, 0, 1389, 19219, '', NULL, 95, '2011-05-02 14:24:01', '2011-05-13 10:40:51'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2530349, 415, '加载 XML 文件时出错。(未找到,代码:404) ', 256, 5, '2011-05-06 00:00:27'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2531615, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-06 02:48:49'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2532882, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-06 05:36:05'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2534150, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-06 08:16:10'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2535418, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-06 11:04:05'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2536684, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-06 13:56:07'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2537951, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-06 16:44:20'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2539219, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-06 19:24:07'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2540489, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-06 21:56:04'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2541758, 415, '加载 XML 文件时出错。(未找到,代码:404) ', 256, 5, '2011-05-07 00:40:04'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2543027, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-07 03:24:01'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2544298, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-07 06:00:07'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2545566, 415, '加载 XML 文件时出错。(未找到,代码:404) ', 256, 5, '2011-05-07 08:48:05'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2546836, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-07 11:24:19'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2548106, 415, '加载 XML 文件时出错。(未找到,代码:404) ', 256, 5, '2011-05-07 14:00:09'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2549374, 415, '加载 XML 文件时出错。(未找到,代码:404) ', 256, 5, '2011-05-07 16:44:04'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2550643, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-07 19:24:01'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2551912, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-07 22:04:02'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2553181, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-08 00:44:04'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2554451, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-08 03:20:10'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2555719, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-08 06:04:05'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2556987, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-08 08:48:05'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2558256, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-08 11:28:06'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2559523, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-08 14:20:04'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2560791, 415, '加载 XML 文件时出错。(未找到,代码:404) ', 256, 5, '2011-05-08 17:08:04'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2562060, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-08 19:48:05'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2563328, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-08 22:28:02'); INSERT INTO `portal_gm_cron` (`id`, `id_wms`, `error`, `errorno`, `flag`, `time`) VALUES (2564598, 415, 'Error loading XML file. (Not Found, Code: 404) ', 256, 5, '2011-05-09 01:04:39'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2531247、1389、NULL、0、5、'2011-05-06 01: 36:55'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2532514、1389、NULL、0、5、'2011-05-06 04: 24:25'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2533782、1389、NULL、0、5、'2011-05-06 07: 04:47'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2535050、1389、NULL、0、5、'2011-05-06 09: 48:36'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2536316、1389、NULL、0、5、'2011-05-06 12: 44:14'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2537582、1389、NULL、0、5、'2011-05-06 15: 36:12'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2538850、1389、NULL、0、5、'2011-05-06 18: 16:10'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2541389、1389、NULL、0、5、'2011-05-06 23: 28:48'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2542660、1389、NULL、0、5、'2011-05-07 02: 04:43'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2543929、1389、NULL、0、5、'2011-05-07 04: 48:38'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2545198、1389、NULL、0、5、'2011-05-07 07: 32:29'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2546468、1389、NULL、0、5、'2011-05-07 10: 12:55'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2547737、1389、NULL、0、5、'2011-05-07 12: 52:45'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2549006、1389、NULL、0、5、'2011-05-07 15: 32:34'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2550275、1389、NULL、0、5、'2011-05-07 18: 08:02'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2551545、1389、NULL、0、5、'2011-05-07 20: 48:32'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2552813、1389、NULL、0、5、'2011-05-07 23: 32:46'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2554083、1389、NULL、0、5、'2011-05-08 02: 08:56'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2555351、1389、NULL、0、5、'2011-05-08 04: 52:26'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2556621、1389、NULL、0、5、'2011-05-08 07: 28:48'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2557888、1389、NULL、0、5、'2011-05-08 10: 16:33'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2559156、1389、NULL、0、5、'2011-05-08 13: 00:21'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2560423、1389、NULL、0、5、'2011-05-08 15: 52:43'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2561692、1389、NULL、0、5、'2011-05-08 18: 36:16'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2562960、1389、NULL、0、5、'2011-05-08 21: 16:01'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2564229、1389、NULL、0、5、'2011-05-08 23: 56:02'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2565498、1389、NULL、0、5、'2011-05-09 02: 32:19'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2566768、1389、NULL、0、5、'2011-05-09 05: 08:43'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2568037、1389、NULL、0、5、'2011-05-09 07: 48:32'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2569304、1389、NULL、0、5、'2011-05-09 10: 40:46'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2570571、1389、NULL、0、5、'2011-05-09 13: 28:45'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2571838、1389、NULL、0、5、'2011-05-09 16: 16:36'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2573106、1389、NULL、0、5、'2011-05-09 18: 56:10'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2574373、1389、NULL、0、5、'2011-05-09 21: 40:38'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2575640、1389、NULL、0、5、'2011-05-10 00: 24:09'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2576906、1389、NULL、0、5、'2011-05-10 03: 16:30'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2578175、1389、NULL、0、5、'2011-05-10 05: 52:55'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2579443、1389、NULL、0、5、'2011-05-10 08: 32:28'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2580711、1389、NULL、0、5、'2011-05-10 11: 20:34'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2581979、1389、NULL、0、5、'2011-05-10 14: 00:46'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2583239、1389、NULL、0、5、'2011-05-10 16: 32:32'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2584511、1389、NULL、0、5、'2011-05-10 19: 04:14'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2585784、1389、NULL、0、5、'2011-05-10 21: 29:00'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2587056、1389、NULL、0、5、'2011-05-10 23: 56:21'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2589600、1389、NULL、0、5、'2011-05-11 04: 56:32'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2590873、1389、NULL、0、5、'2011-05-11 07: 20:50'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2592143、1389、NULL、0、5、'2011-05-11 10: 00:47'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2593414、1389、NULL、0、5、'2011-05-11 12: 36:19'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2594682、1389、NULL、0、5、'2011-05-11 15: 28:39'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2595953、1389、NULL、0、5、'2011-05-11 18: 04:23'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2597226、1389、NULL、0、5、'2011-05-11 20: 28:09'); 插入`portal_gm_cron`(`id`、`id_wms`、`error`、`errorno`、`flag`、`time`)值(2598498、1389、NULL、0、5、'2011-05-11 22: 56:59');

【问题讨论】:

这三个表是否以某种方式相互关联?如果是,则使用 JOINS。如果不是,请使用 UNION。 你能指定你想要的输出是什么样的吗? 【参考方案1】:

不确定我是否完全理解您的意思,但我认为您要求使用每个表中的所有列将三个表中的每一行显示为一个(为其他表中的列放置空值)并进行排序所有行都按日期顺序排列(最近的在前)。

以您的代码为例(选择日期列和过滤器),应该这样做:

SELECT o.*
FROM (
(SELECT id_wms, time AS ordered_time, id, error, errorno, flag, NULL AS code, NULL AS subcode, NULL AS id_layer, NULL AS message, NULL AS message_real, NULL AS counter, NULL AS time_first, NULL AS time_last, NULL AS user_id, NULL AS mail_notify, NULL AS mail_period, NULL AS time_cron
FROM
    portal_gm_cron)
UNION ALL
(SELECT id_wms, time_first AS ordered_time, id, NULL AS error, NULL AS errorno, NULL AS flag, code, subcode, id_layer, message, message_real, counter, time_first, time_last, NULL AS user_id, NULL AS mail_notify, NULL AS mail_period, NULL AS time_cron
FROM
    portal_gm_log)
UNION ALL
(SELECT wms_id as id_wms, time AS ordered_time, id, NULL AS error, NULL AS errorno, NULL AS flag, NULL AS code, NULL AS subcode, NULL AS id_layer, NULL AS message, NULL AS message_real, NULL AS counter, NULL AS time_first, NULL AS time_last, user_id, mail_notify, mail_period, time_cron
FROM
    portal_gm_provide
WHERE
    user_id = 1) 
) AS o
ORDER BY ordered_time DESC

您可以更改列顺序和日期列选择以适合。

【讨论】:

以上是关于MySQL JOIN 和 ORDER 问题(对多个表进行排序)的主要内容,如果未能解决你的问题,请参考以下文章

更改mysql join结果集中的ID值

MySQL 不使用带有 JOIN、WHERE 和 ORDER 的索引

使用 ORDER BY 和 INNER JOIN 优化 MySQL 查询(选择用户关注的位置)

在 JOIN 值上完成时 MySQL 慢 ORDER BY?

mysql数据库多个表union all查询并排序的结果为啥错误

使用 JOIN 优化 SQL 查询的 ORDER BY 和 WHERE