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

标签 mysql

我需要一些提示来查询每个页面中的 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/

相关文章:

c# - 数据库中的值在文本框中不可见,请将值插入到正确的用户名中

php - 删除 MySQL PHP

mysql - hibernate 分离条件 - 如何添加 native sql 语句

php - 如何在PHP中将表中的记录转换为xml格式?

mysql - 为什么 "explain"返回的行不等于 count()?

php - 逃离 MySQL PHP

mysql - 为什么与 docker 容器的 SSH 连接不起作用?

mysql - mysql 中的派生表显示错误

Mysql表串行生成

mysql - 涉及多个表的一对一关系