为什么同一个查询在 Windows MySQL 实例上可以工作,但在 Ubuntu MySQL 实例上却不起作用?
实例版本非常接近:
Windows: @@sql_mode:STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
Ubuntu: @@sql_mode:ONLY_FULL_GROUP_BY、STRICT_TRANS_TABLES、NO_ZERO_IN_DATE、NO_ZERO_DATE、ERROR_FOR_DIVISION_BY_ZERO、NO_ENGINE_SUBSTITUTION
Ubuntu 上的错误:
错误 3065 (HY000): ORDER BY 子句的表达式 #1 不在 SELECT 列表中,引用列 'p.priority' 不在 SELECT 列表中;这与 DISTINCT 不兼容
查询:
SELECT DISTINCT
`id`, `date_created`, `date_last_update`, `text_id_map`, `address_search_aid`, `lat`, `lng`,
`street_line1`, `street_line2`, `zip`, `company_status`, `compassion_level`, `emails`,
`phones`, `social_media`, `urls`, `description`, `importance`, `name`, `object_status`,
`search_aid`, `short_name`, `created_by_id`, `last_update_by_id`, `alternate_country_id`,
`city_id`, `parent_id`, `city_region_id`, `autocomplete`, `sitemap_xml`
FROM (
SELECT p.*, 0 AS `priority`, CASE WHEN `parent_id` IS NULL THEN 1 ELSE 0 END AS `is_parent`
FROM `provider` p
CROSS JOIN JSON_TABLE( JSON_KEYS(`name`), '$[*]' COLUMNS (locale VARCHAR(10) PATH '$') ) AS l
CROSS JOIN JSON_TABLE( JSON_EXTRACT(`name`, CONCAT('$.', l.locale)), '$' COLUMNS (`jsonval` VARCHAR(2048) PATH '$') ) AS j
WHERE
INSTR( CONCAT(' ', REPLACE(j.jsonval, '-', ' '), ' '), CONCAT(' ', 'web', ' ') ) > 0
AND
INSTR( CONCAT(' ', REPLACE(j.jsonval, '-', ' '), ' '), CONCAT(' ', 'kon', ' ') ) > 0
UNION
SELECT *,
CASE
WHEN
INSTR( CONCAT(' ', `autocomplete`, ' '), CONCAT(' ', 'web', ' ') ) > 0
AND
INSTR( CONCAT(' ', `autocomplete`, ' '), CONCAT(' ', 'kon', ' ') ) > 0
THEN 1
WHEN INSTR( `autocomplete`, 'web' ) > 0 AND INSTR( `autocomplete`, 'kon' ) > 0 THEN 2
ELSE 3
END `priority`,
CASE
WHEN parent_id IS NULL THEN 1
ELSE 0
END AS `is_parent`
FROM `provider`
) p
WHERE `priority` < 3
ORDER BY `priority`, `is_parent` DESC
LIMIT 10;
表是这样创建的:
CREATE TABLE `provider` (
`id` int NOT NULL,
`date_created` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`date_last_update` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`text_id_map` text COLLATE utf8mb4_unicode_ci,
`address_search_aid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`lat` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`lng` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`street_line1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`street_line2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`zip` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`company_status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`compassion_level` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`emails` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`phones` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`social_media` text COLLATE utf8mb4_unicode_ci,
`urls` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`description` text COLLATE utf8mb4_unicode_ci,
`importance` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`name` text COLLATE utf8mb4_unicode_ci,
`object_status` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`search_aid` text COLLATE utf8mb4_unicode_ci,
`short_name` text COLLATE utf8mb4_unicode_ci,
`created_by_id` int DEFAULT NULL,
`last_update_by_id` int DEFAULT NULL,
`alternate_country_id` int DEFAULT NULL,
`city_id` int DEFAULT NULL,
`parent_id` int DEFAULT NULL,
`city_region_id` int DEFAULT NULL,
`autocomplete` text COLLATE utf8mb4_unicode_ci,
`sitemap_xml` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`id`),
KEY `FKotfd80u3xayhxxjrvtpclqwe6` (`created_by_id`),
KEY `FK7gqnbcylk4ghwciki0779g32u` (`last_update_by_id`),
KEY `FKk6jlj4kwu3hi5kl4a4qqso7j3` (`alternate_country_id`),
KEY `FKrlh8uhcluf8si4vfbgdw3w6p1` (`city_id`),
KEY `FK6psluxn6a0b64bcxggfuum2l0` (`parent_id`),
KEY `FKegsn16mm766i6j5du01dlvn6` (`city_region_id`),
CONSTRAINT `FK6psluxn6a0b64bcxggfuum2l0` FOREIGN KEY (`parent_id`) REFERENCES `provider` (`id`),
CONSTRAINT `FK7gqnbcylk4ghwciki0779g32u` FOREIGN KEY (`last_update_by_id`) REFERENCES `veg_user` (`id`),
CONSTRAINT `FKegsn16mm766i6j5du01dlvn6` FOREIGN KEY (`city_region_id`) REFERENCES `city_region` (`id`),
CONSTRAINT `FKk6jlj4kwu3hi5kl4a4qqso7j3` FOREIGN KEY (`alternate_country_id`) REFERENCES `country` (`id`),
CONSTRAINT `FKotfd80u3xayhxxjrvtpclqwe6` FOREIGN KEY (`created_by_id`) REFERENCES `veg_user` (`id`),
CONSTRAINT `FKrlh8uhcluf8si4vfbgdw3w6p1` FOREIGN KEY (`city_id`) REFERENCES `city` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Windows 上 Heidi 的屏幕截图;查询从这个问题复制:
SQL 模式
ONLY_FULL_GROUP_BY
影响查询的执行方式。
https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html 说:
...如果任何
表达式不满足以下条件中的至少一个,则具有DISTINCT
和ORDER BY
的查询将被拒绝为无效:ORDER BY
表达式等于选择列表中的1
表达式引用的所有列以及属于查询的选定表的列都是选择列表的元素
阅读我链接到的手册页,以获取有关此规则存在原因的更多说明和示例。
如果您的 SQL 模式省略
ONLY_FULL_GROUP_BY
,则此规则无效,但这会导致您的查询出现任意结果。
我建议保持
ONLY_FULL_GROUP_BY
模式有效,并在 Windows 实例上恢复此选项。从 MySQL 5.7.5 开始默认启用。这对于 MySQL 按照标准 SQL 运行非常重要。
大多数其他品牌的 SQL 都强制执行相同的规则,但没有选项可以禁用它。
您可以通过在选择列表中包含
ORDER BY
: 中引用的列来修复查询
SELECT DISTINCT
`id`, `date_created`, `date_last_update`, `text_id_map`, `address_search_aid`, `lat`, `lng`,
`street_line1`, `street_line2`, `zip`, `company_status`, `compassion_level`, `emails`,
`phones`, `social_media`, `urls`, `description`, `importance`, `name`, `object_status`,
`search_aid`, `short_name`, `created_by_id`, `last_update_by_id`, `alternate_country_id`,
`city_id`, `parent_id`, `city_region_id`, `autocomplete`, `sitemap_xml`,
`priority`, `is_parent` <-- add these columns
...