SELECT * FROM foo
给我类似的东西
name | price | value
foo | 1 | 3
foo | 2 | 2
我要接收:
name | price | value | record
foo | 1 | 3 | row1
foo | 1 | 3 | row2
foo | 1 | 3 | row3
foo | 2 | 2 | row1
foo | 2 | 2 | row2
我该怎么做?
答案 0 :(得分:1)
好的,我试图了解您想要什么,您只想按价格和价值对行进行排序,对吗?如果是这样,只需使用order by
子句,如下所示:
select * from foo order by price, value;
好的,重新编辑我的答案,如下所示,如果您使用PostgreSQL,事情将会非常简单。
select * from foo;
name | price | value
------+-------+-------
foo | 1 | 3
foo | 2 | 2
(2 rows)
select
name,
price,
generate_series(1,value,1) as series_number
from
foo;
name | price | series_number
------+-------+---------------
foo | 1 | 1
foo | 1 | 2
foo | 1 | 3
foo | 2 | 1
foo | 2 | 2
(5 rows)
select name,price,value from (
select
name,
price,
value,
generate_series(1,value,1) as series_number
from
foo
) tmp order by price, value;
name | price | value
------+-------+-------
foo | 1 | 3
foo | 1 | 3
foo | 1 | 3
foo | 2 | 2
foo | 2 | 2
(5 rows)
MySQL完成此操作的方法似乎要复杂得多,请参见此处:https://dba.stackexchange.com/questions/75785/how-to-generate-a-sequence-in-mysql