LIKE CONCAT中奇怪的MySQL 5.7行为
我在Mysql 5.7中遇到了一个非常奇怪的行为。它不会发生在5.6或8.0上,只有5.7 我有一个疑问:LIKE CONCAT中奇怪的MySQL 5.7行为,mysql,collation,mysql-5.7,Mysql,Collation,Mysql 5.7,我在Mysql 5.7中遇到了一个非常奇怪的行为。它不会发生在5.6或8.0上,只有5.7 我有一个疑问: SELECT r.identificador, rh.id_reserva_habitacion, h.id_habitacion, (SELECT identificador FROM reservas WHERE identificador LIKE CONCAT(r.identificador, '.%') LIMIT 1) AS modificada FROM
SELECT
r.identificador, rh.id_reserva_habitacion, h.id_habitacion,
(SELECT identificador FROM reservas WHERE identificador LIKE CONCAT(r.identificador, '.%') LIMIT 1) AS modificada
FROM
(`reservas` r, `reservas_habitaciones` rh, `habitaciones` h)
WHERE
r.identificador = 'XXA' AND r.identificador = rh.identificador AND h.id_habitacion = rh.id_habitacion
GROUP BY
rh.id_reserva_habitacion
这是测试它的最小转储:
CREATE TABLE IF NOT EXISTS `habitaciones` (
`id_habitacion` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_hotel` int(10) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`id_habitacion`)
) ENGINE=MyISAM AUTO_INCREMENT=4474 DEFAULT CHARSET=utf8;
INSERT INTO `habitaciones` (`id_habitacion`, `id_hotel`) VALUES
(1, 200),
(2, 200);
CREATE TABLE IF NOT EXISTS `reservas` (
`identificador` varchar(13) NOT NULL DEFAULT '',
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`identificador`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `reservas` (`identificador`, `timestamp`) VALUES
('XXA', '2020-01-02'),
('XXA.01', '2020-01-01'),
('XXB', '2020-01-01');
CREATE TABLE IF NOT EXISTS `reservas_habitaciones` (
`id_reserva_habitacion` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`identificador` varchar(13) NOT NULL DEFAULT '',
`id_habitacion` int(10) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`id_reserva_habitacion`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
INSERT INTO `reservas_habitaciones` (`id_reserva_habitacion`, `identificador`, `id_habitacion`) VALUES
(1, 'XXA', 1),
(2, 'XXB', 1),
(3, 'XXB', 2);
查询应在modificada
字段上返回值“XXA.01”,但返回NULL
现在是奇怪的部分:
- 如果我只是从查询中删除
,它就可以工作了李>groupby
- 如果我只是从查询中删除
表,它就可以工作了李>habitaciones
- 如果'XXA.01'的值改为'XXA_01',(在查询中为'\u%'),则它可以工作`
- 如果不是
而是CONCAT(r.identificator,'.%')
,则它可以工作CONCAT('XXA','.%')
- 如果我指定了一个排序规则(它应该已经有了),它就可以工作了
- 如果我删除了
中的第二条记录,只留下id“1”,它也可以工作李>habitaciones
- 如果我在5.6或8.0中使用它,它将完美地工作(事实上,它在5.6中一直工作,没有任何问题)
谢谢 您应该从(
reservas
r,reservas\u habitaciones
rh,habitaciones
h)更改为加入。。在…**上,结果与JOINSPLE相同。请为两个版本提供EXPLAIN-SELECT…
和EXPLAIN-FORMAT=JSON-SELECT…
。考虑在BugsMySQL网站上编写一个bug报告。在MariaDB的几个版本上也是可以的。
SELECT
r.identificador, rh.id_reserva_habitacion, h.id_habitacion,
(SELECT identificador FROM reservas WHERE identificador LIKE CONCAT(r.identificador, '.%') LIMIT 1) AS modificada
FROM
(`reservas` r, `reservas_habitaciones` rh, `habitaciones` h)
WHERE
r.identificador = 'XXA' AND r.identificador = rh.identificador AND h.id_habitacion = rh.id_habitacion
SELECT
r.identificador, rh.id_reserva_habitacion,
(SELECT identificador FROM reservas WHERE identificador LIKE CONCAT(r.identificador, '.%') LIMIT 1) AS modificada
FROM
(`reservas` r, `reservas_habitaciones` rh)
WHERE
r.identificador = 'XXA' AND r.identificador = rh.identificador
GROUP BY
rh.id_reserva_habitacion
SELECT
r.identificador, rh.id_reserva_habitacion, h.id_habitacion,
(SELECT identificador FROM reservas WHERE identificador LIKE CONCAT(r.identificador, '_%') LIMIT 1) AS modificada
FROM
(`reservas` r, `reservas_habitaciones` rh, `habitaciones` h)
WHERE
r.identificador = 'XXA' AND r.identificador = rh.identificador AND h.id_habitacion = rh.id_habitacion
GROUP BY
rh.id_reserva_habitacion
SELECT
r.identificador, rh.id_reserva_habitacion, h.id_habitacion,
(SELECT identificador FROM reservas WHERE identificador LIKE CONCAT('XXA', '.%') LIMIT 1) AS modificada
FROM
(`reservas` r, `reservas_habitaciones` rh, `habitaciones` h)
WHERE
r.identificador = 'XXA' AND r.identificador = rh.identificador AND h.id_habitacion = rh.id_habitacion
GROUP BY
rh.id_reserva_habitacion
SELECT
r.identificador, rh.id_reserva_habitacion, h.id_habitacion,
(SELECT identificador FROM reservas WHERE identificador COLLATE utf8_general_ci LIKE CONCAT(r.identificador, '.%') LIMIT 1) AS modificada
FROM
(`reservas` r, `reservas_habitaciones` rh, `habitaciones` h)
WHERE
r.identificador = 'XXA' AND r.identificador = rh.identificador AND h.id_habitacion = rh.id_habitacion
GROUP BY
rh.id_reserva_habitacion