分页与联合和两个子查询

时间:2019-04-10 04:42:44

标签: 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条记录的普通产品

1 个答案:

答案 0 :(得分:0)

尝试一下。这将结合您的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个特色项目并将信息存储在会话中。这样,当用户从一个页面转到另一个页面时,您不必重新查询那些特色项目。您可以从用户会话中获取该信息。非特色产品可以单独查询。

如果特色产品较少,并且想要替代特色产品

如果特色项目少于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