如何限制/偏移每个唯一 ID 返回几行的查询?
我的问题是我需要对此查询中的数据进行分页:
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
但无法使其工作。
我的目标是按上述方式进行查询,但通过计算用户数的行数(而不是所有行)来限制和偏移查询。
有什么想法吗?
My issue is that I need to paginate data from this query:
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;
}
A sample data table / query response would be:
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
If I enter a limit and offset at the end of MySQL query
...
LIMIT $limit
OFFSET $offset;
");
the limit and offset are applied to the total number of rows, not the the number of rows grouped by user.
I've tried using GROUP BY
but was unable to make it work.
My goal is to make the query as above but LIMIT and OFFSET the query by a number of rows that counts users, not all rows.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案实际上是使用 GROUP BY 子句:
顺序很重要。首先是
GROUP BY
,然后是ORDER BY
,然后是OFFSET
/LIMIT
。注意到上面的
SQL_CALC_FOUND_ROWS
了吗?查询执行后,如果您想获取总行数(包括由于LIMIT
子句而未返回的行数),只需使用:并获取
count
栏目。但是,正如您所说,行将折叠,并且您将丢失一些
cars_name
和cars_id
值。另一个解决方案是使用 GROUP_CONCAT,然后在 PHP 中拆分它:
这会给你类似的东西:
The solution really is to use a
GROUP BY
clause:The order is important.
GROUP BY
first, thenORDER BY
, and thenOFFSET
/LIMIT
.Notice the
SQL_CALC_FOUND_ROWS
up there? After the query has executed, if you want to get the total row count (including those who aren't returned because of theLIMIT
clause), just use:And fetch the
count
column.However, like you said, the rows will collapse and you will lose some
cars_name
andcars_id
values.Another solution is to use GROUP_CONCAT, then split it in PHP:
This would give you something like:
如果您想获得这样的列表,
请忘记限制,请执行以下操作:
A - 首先检索用户 id 列表并将其插入临时表中
B - 接下来将相关 user_id 插入表中。
最低的user_id是temptable中的last_insert_id,而temp_table
项目按顺序排列。
C - 将 SQL @var @current_id 设置为 temp_table 中的last_insert_id。
D - 接下来仅使用您想要的 user_id 从表中选择相关行。
E - 现在降低 @current_id
F - 并重复步骤 D 和 E,直到没有更多行为止。
第一个字段
row_count
告诉您字段中聚合的行数car_names
和car_ids
。您可以使用 php 的explode
来分隔这些字段。If you want to get a list like this
Forget about limit, do this instead:
A - First retrieve a list of user id's and insert that into a temp table
B - Next insert the relavant user_id's into the table.
The lowest user_id is the last_insert_id in the temptable, and the temp_table
items are in sequential order.
C - Set the SQL @var @current_id to the last_insert_id in the temp_table.
D - Next select relevant rows from the table, using only the user_id you want.
E - Now lower the @current_id
F - And repeat step D and E until there's no more rows to be had.
The first field
row_count
tells you the number of rows aggregated in the fieldscar_names
andcar_ids
. You can separate these fields by using php'sexplode
.我看不到一种方法可以在一个查询中执行此操作。我的解决方案是使用具有相同参数的分组查询来获取唯一 ID 的计数:
然后获取
uid_countmysql_num_rows
并使用它来计算上述的分页变量询问。I don't see a way to do this in one query. My solution would be to get the count of unique ID's using a group by query with the same parameters:
Then fetch the
uid_countmysql_num_rows
and use that to calculate pagination variables for the above query.