gpt4 book ai didi

mysql - 联合分页和两个子查询

转载 作者:行者123 更新时间:2023-11-30 21:32:14 26 4
gpt4 key购买 nike

我需要一些提示来查询每个页面中的 5 个特色产品(我需要每页呈现 11 个,5 个特色产品和 6 个普通产品),我尝试过联合但是当我将外部限制更改为LIMIT 5 OFFSET 5(第 2 页),没有显示特色产品,是不是我错过了什么?

我试过这个查询:

SELECT * FROM ( 
(SELECT a.id, concat(city.description, '-', department.description) as location, a.featured, a.price, a.title, a.description FROM ads a
LEFT JOIN city on a.city_id = city.id
LEFT JOIN department on city.department_id = department.id
WHERE featured = true order by a.price limit 5 offset 0)
union all
SELECT a.id, concat(city.description, '-', department.description) as location, a.featured, a.price, a.title, a.description FROM ads a
LEFT JOIN city on a.city_id = city.id
LEFT JOIN department on city.department_id = department.id
WHERE featured = false
) as a
ORDER BY featured desc LIMIT 11 OFFSET 0

我期待下一个特色产品和普通产品的输出,但总共只有 6 条记录才获得正常

最佳答案

试试这个。这将组合您的 5 个特色产品和 6 个非特色产品,并设置特色产品的优先级,并首先按特色产品排序。您的应用程序可以获取前 5 个结果并将其显示在每个页面的顶部。

select * from (
SELECT 1 as priority, a.id, concat(city.description, '-', department.description) as location, a.featured, a.price, a.title, a.description
FROM ads a
LEFT JOIN city on a.city_id = city.id
LEFT JOIN department on city.department_id = department.id
WHERE featured = true
ORDER BY a.price
LIMIT 5 offset 0
) x
union all
select * from (
SELECT 2 as priority, a.id, concat(city.description, '-', department.description) as location, a.featured, a.price, a.title, a.description
FROM ads a
LEFT JOIN city on a.city_id = city.id
LEFT JOIN department on city.department_id = department.id
WHERE featured = false
ORDER BY a.price
LIMIT 6 offset 0
) y
order by priority

您还可以尝试另一种方法。如果您希望每个页面都有特色商品,最好分别查询 5 个特色商品并将信息存储在一个 session 中。这样,当用户从一个页面转到另一个页面时,您不必重新查询那些特色项目。您可以从用户的 session 中提取该信息。非特色商品可单独查询。

如果您的特色商品较少,想替换特色商品

如果特色商品少于 5 个,则应在变量中对其进行跟踪。然后,运行:

select * from (
SELECT 1 as priority, a.id, concat(city.description, '-', department.description) as location, a.featured, a.price, a.title, a.description
FROM ads a
LEFT JOIN city on a.city_id = city.id
LEFT JOIN department on city.department_id = department.id
WHERE featured = false
ORDER BY a.price
LIMIT 5 - <ENTER-YOUR-VARIABLE-HERE> offset 0
) y

将特色产品与替代的非特色产品相结合,得到 5 件商品。

然后运行最后一个查询以拉取 6 个非特色产品:

select * from (
SELECT 2 as priority, a.id, concat(city.description, '-', department.description) as location, a.featured, a.price, a.title, a.description
FROM ads a
LEFT JOIN city on a.city_id = city.id
LEFT JOIN department on city.department_id = department.id
WHERE featured = false
ORDER BY a.price
LIMIT 6 offset 0
) y

如果您的特色产品较少并且不想替换特色产品

如果您的特色商品少于 5 个(例如,只有 3 个),您应该在变量中对其进行跟踪。然后,运行:

select * from (
SELECT 2 as priority, a.id, concat(city.description, '-', department.description) as location, a.featured, a.price, a.title, a.description
FROM ads a
LEFT JOIN city on a.city_id = city.id
LEFT JOIN department on city.department_id = department.id
WHERE featured = false
ORDER BY a.price
LIMIT 11 - <ENTER-YOUR-VARIABLE-HERE> offset 0
) y

关于mysql - 联合分页和两个子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55604858/

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