新建这样一个表:
1】表有两列,列名分别为:index_value,row_value;
2】index_value用来保存序号,需要自己插入;row_value用来保存另一个表格中的一列中的内容,如:年龄段
但在另一个表中,年龄段是重复出现的,
user_age
13-22岁
60岁以上
41-60岁
41-60岁
41-60岁
0-12岁
23-40岁
13-22岁
13-22岁
41-60岁
60岁以上
41-60岁
0-12岁
41-60岁
23-40岁
41-60岁
13-22岁
23-40岁
13-22岁 我只需要不重复且都出现的年龄段。如:
0-12岁
13-22岁
23-40岁
41-60岁
60岁以上
需要将这样的列的内容放进新建表的row_value中。现在大概的思路是:
先用insert将index_value的数值填满,再用update将row_value的数值填满,但我不知道具体函数怎么实现。
希望大家给找个方法。
1】表有两列,列名分别为:index_value,row_value;
2】index_value用来保存序号,需要自己插入;row_value用来保存另一个表格中的一列中的内容,如:年龄段
但在另一个表中,年龄段是重复出现的,
user_age
13-22岁
60岁以上
41-60岁
41-60岁
41-60岁
0-12岁
23-40岁
13-22岁
13-22岁
41-60岁
60岁以上
41-60岁
0-12岁
41-60岁
23-40岁
41-60岁
13-22岁
23-40岁
13-22岁 我只需要不重复且都出现的年龄段。如:
0-12岁
13-22岁
23-40岁
41-60岁
60岁以上
需要将这样的列的内容放进新建表的row_value中。现在大概的思路是:
先用insert将index_value的数值填满,再用update将row_value的数值填满,但我不知道具体函数怎么实现。
希望大家给找个方法。
insert into table1(index_value,row_value)
select
(select count(distinct user_age) from table2 where user_age<=t.user_age) as index_value
t.user_age as row_value,
from
(select disctinct user_age from table2) t
insert tb(row_value) select distinct user_age from ta
declare @i int
set @I=0
update tb set index_value=@i,@i=@i+1
if object_id('[ta]') is not null drop table [ta]
go
create table [ta]([index_value] int identity(1,1),[row_value] varchar(10))if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([user_age] varchar(10))
insert [tb]
select '13-22岁' union all
select '60岁以上' union all
select '41-60岁' union all
select '41-60岁' union all
select '41-60岁' union all
select '0-12岁' union all
select '23-40岁' union all
select '13-22岁' union all
select '13-22岁' union all
select '41-60岁' union all
select '60岁以上' union all
select '41-60岁' union all
select '0-12岁' union all
select '41-60岁' union all
select '23-40岁' union all
select '41-60岁' union all
select '13-22岁' union all
select '23-40岁' union all
select '13-22岁'
---数据插入表---
insert into ta(row_value)
select distinct user_age from tb---查询---
select * from ta---结果---
index_value row_value
----------- ----------
1 0-12岁
2 13-22岁
3 23-40岁
4 41-60岁
5 60岁以上(所影响的行数为 5 行)
请问这个是在什么环境下?是sql server吗
是的,你的index_value取值有什么要求?
create table table1(index_value int,row_value varchar(16))
create table table2(user_age varchar(16))
insert into table2 select '13-22岁 '
insert into table2 select '60岁以上'
insert into table2 select '41-60岁 '
insert into table2 select '41-60岁 '
insert into table2 select '41-60岁 '
insert into table2 select '0-12岁 '
insert into table2 select '23-40岁 '
insert into table2 select '13-22岁 '
insert into table2 select '13-22岁 '
insert into table2 select '41-60岁 '
insert into table2 select '60岁以上'
insert into table2 select '41-60岁 '
insert into table2 select '0-12岁 '
insert into table2 select '41-60岁 '
insert into table2 select '23-40岁 '
insert into table2 select '41-60岁 '
insert into table2 select '13-22岁 '
insert into table2 select '23-40岁 '
insert into table2 select '13-22岁 'insert into table1(index_value,row_value)
select
(select count(distinct user_age) from table2 where user_age<=t.user_age) as index_value,
t.user_age as row_value
from
(select distinct user_age from table2) tselect * from table1
/*
index_value row_value
----------- ----------------
1 0-12岁
2 13-22岁
3 23-40岁
4 41-60岁
5 60岁以上
*/drop table table1,table2
(row_value)
SELECT DISTINCT user_age
FROM current_info_table DECLARE @i int
SET @I = 0
UPDATE combobox_temp_table
SET index_value = @i, @i = @i + 1
这是我的,但是提示不能解析
这个会有这个提示“FROM 子句错误: “<”附近。
无法分析查询文本。”我是SQL Server 2005
这个虽然提示不能解析,但是却可以在表中加入数据。
但有一个问题:
本来index_value 应该是5个,所以插入的数据应是5条数据才对,可是现在却有10条数据。
情况是这样的:
并不是之前加入的数据,已经将原来有的数据删除了;
会提示“sql语句不能解析”,之后却又提示“影响了10行”,也就是说应该是插入了10行!
可是打开表又只有5行;
可是同样的语句在vc中调用,就会确实插入了10行数据。
太奇怪啦!!
各位帮忙看一下哈!