如何限制/偏移查询,每个唯一ID返回几行?

时间:2011-05-30 18:49:47

标签: php mysql pagination group-by limit

我的问题是我需要对此查询中的数据进行分页:

function search($search_term, $limit, $offset)
{
    $id = $this->auth->get_user_id();

    $query = $this->db->query("
    SELECT user_id,
           first_name,
           cars_name,
           cars_id
    FROM   user_profiles
           LEFT JOIN cars
             ON cars.id_fk = user_id
    WHERE  user_id NOT LIKE '$id'
           AND activated = 1
           AND banned = 0
           AND first_name LIKE '%$search_term%'
    ORDER BY first_name ASC
    ");

    $search_data = array();
    foreach ($query->result() as $row) {

        $search_data[$row->user_id]['name'] = $row->first_name;
        $search_data[$row->user_id]['cars'][$row->cars_id] = array(
                'cars_name' => $row->cars_name);
    }
    return $search_data;
}

示例数据表/查询响应将是:

1   JOE   HONDA    123
1   JOE   TOYOTA   124
2   MAC   VW       125
2   MAC   HONDA    126
2   MAC   TESLA    127
3   STU   SUBARU   128
3   STU   KIA      129

-----------
Page 1
-----------
1   JOE   HONDA   123
          TOYOTA  124

2   MAC   VW      125
          HONDA   126

------------
Page 2
------------
3    STU    SUBARU  128
            KIA     129

如果我在MySQL查询结束时输入限制和偏移量

...
LIMIT $limit
OFFSET $offset;
");

限制和偏移量应用于行数,而不是用户分组的行数。

我已尝试使用GROUP BY但无法使其正常工作。

我的目标是按上述方式进行查询,但LIMIT和OFFSET查询按行数计算,而不是所有行。

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

我没有看到在一个查询中执行此操作的方法。我的解决方案是使用具有相同参数的查询来获取唯一ID的计数:

SELECT COUNT(1) AS uid_count
FROM   user_profiles
       LEFT JOIN cars
         ON cars.id_fk = user_id
GROUP BY user_profiles.user_id
WHERE  user_id NOT LIKE '$id'
       AND activated = 1
       AND banned = 0
       AND first_name LIKE '%$search_term%'

然后获取 uid_count mysql_num_rows并使用它来计算上述查询的分页变量。

答案 1 :(得分:0)

解决方案实际上是使用GROUP BY子句:

SELECT SQL_CALC_FOUND_ROWS
       user_id,
       first_name,
       cars_name,
       cars_id
FROM   user_profiles
       LEFT JOIN cars
         ON cars.id_fk = user_id
WHERE  user_id NOT LIKE '$id'
       AND activated = 1
       AND banned = 0
       AND first_name LIKE '%$search_term%'
GROUP BY user_id
ORDER BY first_name ASC
LIMIT 100

订单很重要。先GROUP BY,然后ORDER BY,然后OFFSET / LIMIT

注意那里的SQL_CALC_FOUND_ROWS?执行查询后,如果要获取总行数(包括因LIMIT子句而未返回的行数),只需使用:

SELECT FOUND_ROWS() AS `count`

并获取count列。

但是,就像你说的那样,行会崩溃,你会丢失一些cars_namecars_id值。

另一种解决方案是使用GROUP_CONCAT,然后将其拆分为PHP:

SELECT 
       user_id,
       first_name,
       GROUP_CONCAT(cars_name SEPARATOR ','),
       GROUP_CONCAT(cars_id SEPARATOR ','),
FROM   user_profiles
       LEFT JOIN cars
         ON cars.id_fk = user_id
WHERE  user_id NOT LIKE '$id'
       AND activated = 1
       AND banned = 0
       AND first_name LIKE '%$search_term%'
ORDER BY first_name ASC
LIMIT 100

这会给你类似的东西:

1   JOE   HONDA,TOYOTA     123,124
2   MAC   VW,HONDA,TESLA   125,126,127
3   STU   SUBARU,KIA       128,129

答案 2 :(得分:0)

如果你想获得这样的列表

Page 1
----------------------
1   JOE   HONDA    123
1   JOE   TOYOTA   124

Page 2
----------------------
2   MAC   VW       125
2   MAC   HONDA    126
2   MAC   TESLA    127

Page 3
----------------------
3   STU   SUBARU   128
3   STU   KIA      129

忘掉限制,改为:

A - 首先检索用户ID列表并将其插入临时表

CREATE TEMPORARY TABLE `test`.`temp_user_ids` (
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = MEMORY

B - 接下来将relavant user_id插入表中。

INSERT INTO temp_user_ids
  SELECT null, user_id
FROM   user_profiles
       LEFT JOIN cars
         ON cars.id_fk = user_id
WHERE  user_id NOT LIKE '$id'
       AND activated = 1
       AND banned = 0
       AND first_name LIKE '%$search_term%'
ORDER BY user_id DESC  /*insert in reverse order !*/

最低的user_id是temptable中的last_insert_id和temp_table 项目按顺序排列。

C - 将SQL @var @current_id设置为temp_table中的last_insert_id。

SELECT @current_id:= LAST_INSERT_ID() 

D - 接下来,仅使用您想要的user_id从表中选择相关行。

SELECT count(*) as row_count,
       up.user_id,
       first_name,
       group_concat(cars_name) as car_names,
       group_concat(cars_id) as car_ids,
FROM   user_profiles up
       LEFT JOIN cars
         ON cars.id_fk = up.user_id
       INNER JOIN temp_user_ids t 
         ON (t.user_id = up.user_id)
WHERE  t.id = @current_id
GROUP BY up.user_id
ORDER BY cars.id

E - 现在降低@current_id

SELECT @current_id:= @current_id - 1;

F - 并重复步骤D和E,直到没有更多的行为止。

第一个字段row_count告诉您字段中聚合的行数
car_namescar_ids。您可以使用php的explode

分隔这些字段