我不想使用Match Against,因为如果连续存在多个关键字,则无法获得我想要查询的相关性。目标是按比赛数量获得订单。
这些是当前用于测试的MySQL数据库的详细信息:
--
-- Base de datos: `libros`
--
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `libros`
--
CREATE TABLE `libros` (
`id_unico` int(11) NOT NULL,
`titulo` text NOT NULL,
`id_libro` text NOT NULL,
`tipo` text NOT NULL,
`posicion` int(11) NOT NULL,
`precio` float(20,2) NOT NULL,
`keywords` text NOT NULL,
`proveedor` text NOT NULL,
PRIMARY KEY (`id_unico`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Volcar la base de datos para la tabla `libros`
--
INSERT INTO `libros` VALUES (1, 'La ladrona de los libros', '9788426419866', 'nuevo', 200, 10.00, 'Ladrona, Libros, Ladron', 'eBay');
INSERT INTO `libros` VALUES (2, 'Los surcos del azar', '9788415685364', 'nuevo', 3, 13.00, 'Surcos, Azar', 'Marcelo');
INSERT INTO `libros` VALUES (3, 'Los surcos del azar', '9788415685364', 'nuevo', 5, 15.00, 'Surcos, Azar', 'Amazon');
INSERT INTO `libros` VALUES (4, 'El castillo', '9788467520170', 'nuevo', 10, 20.00, 'Castillo, Medieval', 'Marcelo');
INSERT INTO `libros` VALUES (5, 'El Castillo', '9788467520170', 'nuevo', 5, 15.00, 'Castillo, Medieval', 'Casa del libro');
INSERT INTO `libros` VALUES (6, 'La jungla de los listos', '9788467040470', 'nuevo', 6, 30.00, 'Jungla, Libros', 'eBay');
INSERT INTO `libros` VALUES (7, 'La jungla de los libros', '9788467040470', 'nuevo', 4, 19.00, 'Jungla, Libros', 'Amazon');
INSERT INTO `libros` VALUES (8, 'La ladrona de los libros', '9788426419866', 'nuevo', 99, 40.00, 'Ladrona, Libros, Ladron, 2014', 'Luis');
INSERT INTO `libros` VALUES (9, 'La ladrona de los libros', '9788426419866', 'nuevo', 21, 22.00, 'Ladrona, Libros, Ladron', 'Lili');
我运行的查询是:
SELECT *, SUM(
IF(keywords LIKE '%Ladrona%', 1, 0) +
IF(keywords LIKE '%Libros%', 1, 0) +
IF(keywords LIKE '%2014%', 1, 0)
) total FROM libros
WHERE tipo = 'nuevo' GROUP BY id_libro HAVING total > 0 ORDER BY total DESC, posicion ASC, precio DESC
我想返回id_unico" 8"关于第一个结果,不能。而是分别返回id_unico 1和6。我该怎么办?
答案 0 :(得分:0)
您不能在SELECT *语句中使用GROUP BY。所有未由聚合函数(SUM,MAX等)计算的列必须位于GROUP BY中。
所以也许你想要的是:
SELECT id_libro, SUM(
IF(keywords LIKE '%Ladrona%', 1, 0) +
IF(keywords LIKE '%Libros%', 1, 0) +
IF(keywords LIKE '%2014%', 1, 0)
) total
FROM libros
WHERE tipo = 'nuevo'
GROUP BY id_libro
HAVING total>0
ORDER BY total DESC
只有当您完全确定具有相同id_libro的所有记录也具有相同的posicion和precio时,您才能执行此操作:
SELECT id_libro, posicion, precio, SUM(
IF(keywords LIKE '%Ladrona%', 1, 0) +
IF(keywords LIKE '%Libros%', 1, 0) +
IF(keywords LIKE '%2014%', 1, 0)
) total
FROM libros
WHERE tipo = 'nuevo'
GROUP BY id_libro, posicion, precio
HAVING total>0
ORDER BY total DESC, posicion ASC, precio DESC
...但这也意味着数据库的设计非常糟糕:(
答案 1 :(得分:0)
试试这个:
SELECT *, (
IF(keywords LIKE '%Ladrona%', 1, 0) +
IF(keywords LIKE '%Libros%', 1, 0) +
IF(keywords LIKE '%2014%', 1, 0)
) total FROM libros
WHERE tipo = 'nuevo' HAVING total > 0
ORDER BY total DESC, posicion ASC, precio DESC
答案 2 :(得分:0)
问题是您按id_libro
进行分组,但每个id_unico
值有多个id_libro
值,因此查询只返回第一个(1)。要获取每个id_libro
分组的值的连锁列表,您可以使用group_concat
:
SELECT
group_concat(id_unico) id_unico,
titulo,
id_libro,
tipo,
group_concat(posicion) posicion,
group_concat(precio) precio,
group_concat(keywords) keywords,
group_concat(proveedor) proveedor,
SUM(
IF(keywords LIKE '%Ladrona%', 1, 0) +
IF(keywords LIKE '%Libros%', 1, 0) +
IF(keywords LIKE '%2014%', 1, 0)
) total
FROM libros
WHERE tipo = 'nuevo'
GROUP BY id_libro
HAVING total > 0
ORDER BY total DESC, posicion ASC, precio DESC