【问题标题】:cakephp pagination with complex query带有复杂查询的cakephp分页
【发布时间】:2016-02-15 02:55:53
【问题描述】:

我有一个下面给出的数据库结构

id | Title   | brand | 
---+---------+-------+
1  |product1 | Lg    |
2  |pruduct2 | Lg    |
3  |pruduct3 | Lg    |
4  |pruduct4 | Lg    |
5  |pruduct5 | Lg    |
6  |pruduct6 |samsung|
7  |pruduct7 |samsung|
8  |pruduct8 |samsung|
9  |pruduct9 |samsung|
10 |pruduct10|samsung|
11 |pruduct11| sony  |
12 |pruduct12| sony  |
13 |pruduct13| sony  |
14 |pruduct14| sony  |
15 |pruduct15| sony  |

我们需要一些查询,通过它我可以按品牌混合结果
输出应该是这样的

id | Title   | brand | 
---+---------+-------+
1  |product1 | Lg    |
6  |pruduct6 |samsung|
11 |pruduct11| sony  |
2  |pruduct2 | Lg    |
7  |pruduct7 |samsung|    
12 |pruduct12| sony  |
.... and so on

我有以下查询可以检索到相同的结果

select id, title,brand row_number() over (partition by brand_id) as 
row_num from product_test order by row_;

不幸的是,这不适用于 MySql 需要基于 cakephp find 或任何其他方法的解决方案

提前致谢

【问题讨论】:

  • 不清楚您要达到的目标,也没有说明您使用的是哪个版本的 CakePHP。
  • 很清楚需要做什么,但他希望你为他编写代码。 :) 他希望你为他提供的 SQL 查询编写 ORM 代码。

标签: php mysql cakephp pagination cakephp-2.5


【解决方案1】:

您可以通过以下方式模拟查询:

SELECT @rownum := 0, @brand := NULL;

SELECT
  id
, title
, brand
FROM (
  SELECT
    id
  , title
  , brand
  , @rownum := IF(@brand <=> brand, @rownum + 1, 1) AS row_num
  , @brand := brand AS dummy
  FROM
    product_test
  ORDER BY
    brand
  , id
  ) product_test
ORDER BY
  row_num
, brand
, id;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 2013-06-08
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多