gpt4 book ai didi

mysql - LIMIT 不适用于复杂查询

转载 作者:行者123 更新时间:2023-11-29 23:18:37 26 4
gpt4 key购买 nike

我创建了一个函数来生成一个复杂的查询,为我的搜索引擎计算点。它得到了正确的结果,但我无法限制输出行...这是为 2 个单词搜索生成的查询(“lorem ipsum”):

SELECT SQL_CALC_FOUND_ROWS
`Search`.`model` AS `model`,
`Search`.`foreign_key` AS `id`,
SUM(`Search`.`points`) AS `points`
FROM (
SELECT
`i18n`.`foreign_key`, `i18n`.`model`,
(CASE
WHEN `i18n`.`field` = "name" THEN 50
WHEN `i18n`.`field` = "s_desc" THEN 20
END) AS `points`
FROM
`i18n`
WHERE
`i18n`.`field` IN ("name", "s_desc") AND
`i18n`.`locale` = "en-us" AND
(`i18n`.`content` LIKE "lorem%" OR `i18n`.`content` LIKE "% lorem%")

UNION ALL

SELECT
`tagged`.`foreign_key`, `tagged`.`model`,
"50" as `points`
FROM
`tagged`
INNER JOIN
`tags` ON
`tags`.`id` = `tagged`.`tag_id` AND
`tags`.`name` = "lorem"
WHERE
`tagged`.`language` = "en-us"
UNION ALL
SELECT
`i18n`.`foreign_key`, `i18n`.`model`,
(CASE
WHEN `i18n`.`field` = "name" THEN 50
WHEN `i18n`.`field` = "s_desc" THEN 20
END) AS `points`
FROM
`i18n`
WHERE
`i18n`.`field` IN ("name", "s_desc") AND
`i18n`.`locale` = "en-us" AND
(`i18n`.`content` LIKE "ipsum%" OR `i18n`.`content` LIKE "% ipsum%")

UNION ALL

SELECT
`tagged`.`foreign_key`, `tagged`.`model`,
"50" as `points`
FROM
`tagged`
INNER JOIN
`tags` ON
`tags`.`id` = `tagged`.`tag_id` AND
`tags`.`name` = "ipsum"
WHERE
`tagged`.`language` = "en-us"
) `Search`
GROUP BY
`Search`.`model`,
`Search`.`foreign_key`
ORDER BY
`points` DESC;
LIMIT 5 OFFSET 0

请注意,我还尝试了LIMIT 5, 0,并且仅尝试了LIMIT 5。它返回所有命中,而不仅仅是前 5 个。我做错了什么?

最佳答案

现在一切正常,问题出在 ORDER 语句中的 ;

ORDER BY
`points` DESC
LIMIT 5 OFFSET 0

关于mysql - LIMIT 不适用于复杂查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27548848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com