请参阅下表
PID Amount Qty
101 85.00 4
7078 971.25 5
7173 115.50 6
使用上表需要在新表中添加条目,如下所示
PID Amount Qty
101 85.00 4
101 971.25 5
101 115.50 6
7078 85.00 4
7078 971.25 5
7078 115.50 6
7173 85.00 4
7173 971.25 5
7173 115.50 6
建议此条目的SQL查询
答案 0 :(得分:1)
创建表格
create table mytable(PID int, Amount decimal(5,2), Qty int);
insert into mytable values
(101 ,85.00 ,4),
(7078,971.25 ,5),
(7173,115.50 ,6);
create table secondtable(PID int, Amount decimal(5,2), Qty int);
使用Cross Join
,您可以编写如下的插入查询:
insert into secondtable
select m.pid,
m1.amount,
m1.qty
from mytable m
cross join mytable m1;
选择声明:
select * from secondtable
order by pid,qty;
<强>结果:强>
+------+--------+-----+
| PID | Amount | Qty |
+------+--------+-----+
| 101 | 85 | 4 |
| 101 | 971.25 | 5 |
| 101 | 115.5 | 6 |
| 7078 | 85 | 4 |
| 7078 | 971.25 | 5 |
| 7078 | 115.5 | 6 |
| 7173 | 85 | 4 |
| 7173 | 971.25 | 5 |
| 7173 | 115.5 | 6 |
+------+--------+-----+