I made a function that generates a complex query that calculates points for my search engine. It gets the results right, but I cannot limit the output rows... Here's the query generated for a 2-words search ("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
Note that I also tried LIMIT 5, 0
and only LIMIT 5
. It returns all hits, not only the first 5. What am I doing wrong?
Everything is working now, the problem was ;
in the ORDER
statement.
ORDER BY
`points` DESC
LIMIT 5 OFFSET 0
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加