Mysql 如何限制SQL中每个字段值的行数?
例如,我在Hive中有这样一个表:Mysql 如何限制SQL中每个字段值的行数?,mysql,sql,greatest-n-per-group,hive,Mysql,Sql,Greatest N Per Group,Hive,例如,我在Hive中有这样一个表: 1 1 1 4 1 8 2 1 2 5 3 1 3 2 我只想返回第一列中每个唯一值的前两行。我希望它能够限制我从Hive传输到MySQL以用于报告目的的数据量。我想要一个单独的HiveQL查询,它可以提供以下信息: 1 1 1 4 2 1 2 5 3 1 3 2 不幸的是,mysql没有分析功能。所以你必须处理变量。 假设您有一个自动递增字段: mysql> create table mytab ( -> id int not nul
1 1
1 4
1 8
2 1
2 5
3 1
3 2
我只想返回第一列中每个唯一值的前两行。我希望它能够限制我从Hive传输到MySQL以用于报告目的的数据量。我想要一个单独的HiveQL查询,它可以提供以下信息:
1 1
1 4
2 1
2 5
3 1
3 2
不幸的是,mysql没有分析功能。所以你必须处理变量。 假设您有一个自动递增字段:
mysql> create table mytab (
-> id int not null auto_increment primary key,
-> first_column int,
-> second_column int
-> ) engine = myisam;
Query OK, 0 rows affected (0.05 sec)
mysql> insert into mytab (first_column,second_column)
-> values
-> (1,1),(1,4),(2,10),(3,4),(1,4),(2,5),(1,6);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> select * from mytab order by id;
+----+--------------+---------------+
| id | first_column | second_column |
+----+--------------+---------------+
| 1 | 1 | 1 |
| 2 | 1 | 4 |
| 3 | 2 | 10 |
| 4 | 3 | 4 |
| 5 | 1 | 4 |
| 6 | 2 | 5 |
| 7 | 1 | 6 |
+----+--------------+---------------+
7 rows in set (0.00 sec)
mysql> select
-> id,
-> first_column,
-> second_column,
-> row_num
-> from (
-> select *,
-> @num := if(@first_column = first_column, @num:= @num + 1, 1) as row_num,
-> @first_column:=first_column as c
-> from mytab order by first_column,id) as t,(select @first_column:='',@num:
=0) as r;
+----+--------------+---------------+---------+
| id | first_column | second_column | row_num |
+----+--------------+---------------+---------+
| 1 | 1 | 1 | 1 |
| 2 | 1 | 4 | 2 |
| 5 | 1 | 4 | 3 |
| 7 | 1 | 6 | 4 |
| 3 | 2 | 10 | 1 |
| 6 | 2 | 5 | 2 |
| 4 | 3 | 4 | 1 |
+----+--------------+---------------+---------+
7 rows in set (0.00 sec)
mysql> select
-> id,
-> first_column,
-> second_column,
-> row_num
-> from (
-> select *,
-> @num := if(@first_column = first_column, @num:= @num + 1, 1) as row_num,
-> @first_column:=first_column as c
-> from mytab order by first_column,id) as t,(select @first_column:='',@num:
=0) as r
-> having row_num<=2;
+----+--------------+---------------+---------+
| id | first_column | second_column | row_num |
+----+--------------+---------------+---------+
| 1 | 1 | 1 | 1 |
| 2 | 1 | 4 | 2 |
| 3 | 2 | 10 | 1 |
| 6 | 2 | 5 | 2 |
| 4 | 3 | 4 | 1 |
+----+--------------+---------------+---------+
5 rows in set (0.02 sec)
蜂箱解决方案是
SELECT S.col1, S.col2
FROM
(SELECT col1, col2, row_number() over (partition by col1) as r FROM mytable) S
WHERE S.r < 3
这些表和列没有名称吗?请尝试按标记组合搜索此站点,看看是否能找到适合您情况的解决方案。@repalviglator您好,MySQL中的此解决方案很棒!但我想在蜂箱里做。我看到你的标签上有蜂巢,你能告诉我怎么做吗?谢谢!没有ORDERBY子句,1不起作用。2 ORDER BY column必须是您计算的列。否则这就不行了。注意:只适用于mysql>=8