将一个MySQL表中的排序值插入到另一个表中
我有两张表,将一个MySQL表中的排序值插入到另一个表中,mysql,Mysql,我有两张表,average\u table和position\u table。它们各有两列。我想将排序后的数据从average\u表复制到position\u表,如下所示 average_表如下所示 std_id avr 001 23.4 002 34.7 003 13.9 004 56.8 然后位置\u表应如下所示: std_id avr 004 56.8 002 3
average\u table
和position\u table
。它们各有两列。我想将排序后的数据从average\u表
复制到position\u表
,如下所示
average_表
如下所示
std_id avr
001 23.4
002 34.7
003 13.9
004 56.8
然后位置\u表应如下所示:
std_id avr
004 56.8
002 34.7
001 23.4
003 13.9
当我使用以下sql查询时,average\u table
和position\u table
之间的结果没有什么不同。有人能帮我吗
try {
String str = "insert into POSITION_TABLE select * from AVERAGE_TABLE ORDER BY avr DESC";
rs = st.executeQuery(str);
}
catch(SQLException ce){System.out.print(ce)}
在SQL世界中,行并没有真正排序。在MySQL中可能有一种方法可以做到这一点(在SQL server中,我认为集群服务器可以做到这一点——但这是另一个原因,而不是让行按顺序排列)
换句话说,插入的行不能保证具有特定的顺序。由于SQL是声明性的,所以在查询时必须声明如何对其排序
我不明白您试图通过位置
表实现什么-对我来说,它看起来像是平均值
表的精确副本,并且您已经通过使用order by的查询以排序顺序获得了数据。当需要对数据进行排序时,只需使用order by即可。在SQL world中,行并没有真正进行排序。在MySQL中可能有一种方法可以做到这一点(在SQL server中,我认为集群服务器可以做到这一点——但这是另一个原因,而不是让行按顺序排列)
create table average_table
( std_id int not null,
avr decimal(8,2) not null
);
insert average_table (std_id,avr) values (1,23.4),(2,34.7),(3,13.9),(4,56.8);
create table position_table
( id int auto_increment primary key,
std_id int not null,
avr decimal(8,2) not null
);
insert into position_table(std_id,avr) select std_id,avr from average_table order by avr desc;
select * from position_table;
+----+--------+-------+
| id | std_id | avr |
+----+--------+-------+
| 1 | 4 | 56.80 |
| 2 | 2 | 34.70 |
| 3 | 1 | 23.40 |
| 4 | 3 | 13.90 |
+----+--------+-------+
换句话说,插入的行不能保证具有特定的顺序。由于SQL是声明性的,所以在查询时必须声明如何对其排序
我不明白您试图通过位置
表实现什么-对我来说,它看起来像是平均值
表的精确副本,并且您已经通过使用order by的查询以排序顺序获得了数据。当需要对数据进行排序时,只需使用order by即可。在SQL world中,行并没有真正进行排序。在MySQL中可能有一种方法可以做到这一点(在SQL server中,我认为集群服务器可以做到这一点——但这是另一个原因,而不是让行按顺序排列)
create table average_table
( std_id int not null,
avr decimal(8,2) not null
);
insert average_table (std_id,avr) values (1,23.4),(2,34.7),(3,13.9),(4,56.8);
create table position_table
( id int auto_increment primary key,
std_id int not null,
avr decimal(8,2) not null
);
insert into position_table(std_id,avr) select std_id,avr from average_table order by avr desc;
select * from position_table;
+----+--------+-------+
| id | std_id | avr |
+----+--------+-------+
| 1 | 4 | 56.80 |
| 2 | 2 | 34.70 |
| 3 | 1 | 23.40 |
| 4 | 3 | 13.90 |
+----+--------+-------+
换句话说,插入的行不能保证具有特定的顺序。由于SQL是声明性的,所以在查询时必须声明如何对其排序
我不明白您试图通过位置
表实现什么-对我来说,它看起来像是平均值
表的精确副本,并且您已经通过使用order by的查询以排序顺序获得了数据。当需要对数据进行排序时,只需使用order by即可。在SQL world中,行并没有真正进行排序。在MySQL中可能有一种方法可以做到这一点(在SQL server中,我认为集群服务器可以做到这一点——但这是另一个原因,而不是让行按顺序排列)
create table average_table
( std_id int not null,
avr decimal(8,2) not null
);
insert average_table (std_id,avr) values (1,23.4),(2,34.7),(3,13.9),(4,56.8);
create table position_table
( id int auto_increment primary key,
std_id int not null,
avr decimal(8,2) not null
);
insert into position_table(std_id,avr) select std_id,avr from average_table order by avr desc;
select * from position_table;
+----+--------+-------+
| id | std_id | avr |
+----+--------+-------+
| 1 | 4 | 56.80 |
| 2 | 2 | 34.70 |
| 3 | 1 | 23.40 |
| 4 | 3 | 13.90 |
+----+--------+-------+
换句话说,插入的行不能保证具有特定的顺序。由于SQL是声明性的,所以在查询时必须声明如何对其排序
我不明白您试图通过位置
表实现什么-对我来说,它看起来像是平均值
表的精确副本,并且您已经通过使用order by的查询以排序顺序获得了数据。当您需要排序数据时,只需使用order by。当您说world时,您指的是物理?@ddmps那么我如何克服上述问题呢?@Manu实际问题是什么?如果您插入position_表的唯一动机是按位置获取数据,而不像ddmps所说的那样,这只是一个查询,放弃位置表的概念。@ddmps当我运行上面的sql查询时,数据从位置表复制到了avarage表,但没有按需要排序。这两个表包含相同的未排序数据。这就是问题所在当你说“世界”时,你指的是物理的?@ddmps那么我怎样才能克服上述问题呢?@Manu实际的问题是什么?如果你插入position_表的唯一动机是按位置获取数据,而不是像ddmps所说的,那只是一个查询,放弃位置表的概念。@ddmps当我运行上面的sql查询时,数据从位置表复制到了avarage表,但没有按需要排序。这两个表包含相同的未排序数据。这就是问题所在当你说“世界”时,你指的是物理的?@ddmps那么我怎样才能克服上述问题呢?@Manu实际的问题是什么?如果你插入position_表的唯一动机是按位置获取数据,而不是像ddmps所说的,那只是一个查询,放弃位置表的概念。@ddmps当我运行上面的sql查询时,数据从位置表复制到了avarage表,但没有按需要排序。这两个表包含相同的未排序数据。这就是问题所在当你说“世界”时,你指的是物理的?@ddmps那么我怎样才能克服上述问题呢?@Manu实际的问题是什么?如果你插入position_表的唯一动机是按位置获取数据,而不是像ddmps所说的,那只是一个查询,放弃位置表的概念。@ddmps当我运行上面的sql查询时,数据从位置表复制到了avarage表,但没有按需要排序。这两个表包含相同的未排序数据。这就是问题所在
create table average_table
( std_id int not null,
avr decimal(8,2) not null
);
insert average_table (std_id,avr) values (1,23.4),(2,34.7),(3,13.9),(4,56.8);
create table position_table
( id int auto_increment primary key,
std_id int not null,
avr decimal(8,2) not null
);
insert into position_table(std_id,avr) select std_id,avr from average_table order by avr desc;
select * from position_table;
+----+--------+-------+
| id | std_id | avr |
+----+--------+-------+
| 1 | 4 | 56.80 |
| 2 | 2 | 34.70 |
| 3 | 1 | 23.40 |
| 4 | 3 | 13.90 |
+----+--------+-------+