在这种情况下,我想为表中的每一行生成重复的行。重复次数等于每行中的num_rows列。
public class AreaSelect extends AppCompatActivity {
TextView textView;
String union;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_area_select);
textView = findViewById(R.id.tvUnion);
Bundle extras = getIntent().getExtras();
union = extras.getString("key");
textView.setText(union);
}
预期产出:
create table test_create_rows(name varchar2(20), num_rows integer);
insert into test_create_rows values('Name1',3);
insert into test_create_rows values('Name2',4);
insert into test_create_rows values('Name3',1);
commit;
答案 0 :(得分:4)
将cross apply子句与分层查询一起使用
select x.*
from test_create_rows t
cross apply (
select t.NAME, t.NUM_ROWS from dual
connect by level <= t.num_rows
) x;
答案 1 :(得分:3)
您可以尝试使用CTE Recursion
WITH CTE(name, num_rows,startnum) AS(
SELECT name,num_rows,1 startnum
FROM test_create_rows
UNION ALL
SELECT name,num_rows,(startnum + 1) as startnum
FROM CTE
WHERE (startnum + 1) <= num_rows
)
SELECT name ,num_rows
FROM CTE
order by name
<强> Results 强>:
| NAME | NUM_ROWS |
|-------|----------|
| Name1 | 3 |
| Name1 | 3 |
| Name1 | 3 |
| Name2 | 4 |
| Name2 | 4 |
| Name2 | 4 |
| Name2 | 4 |
| Name3 | 1 |