求条SQL:统计数量

soundtbugle 2008-06-19 09:09:22
t1.column1有重复的记录,例如
column1 column2
-------------------
2 1
2 4
1 1
5 10
6 11
5 12

t2.total记录重复的数量,例如
column1 total
--------------------
2 2
1 1
5 2
6 1

我需要一条SQL把total填上值,现在t2
column total
--------------------
2 0
1 0
5 0
6 0
...全文
167 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
iscandy 2008-06-20
  • 打赏
  • 举报
回复
你的 t2 表是多余的
iscandy 2008-06-20
  • 打赏
  • 举报
回复
select column1 as column,count(column1) as total into t2 from t1 group by column1
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 soundtbugle 的回复:]
引用 5 楼 kaiyong 的回复:
得说明t1表跟t2表的关系?

你没看仔细吧!我要把t1的重复数里用t1.column1=t2.column1写到t2
[/Quote]

那为什么变成0了呢???

现在不明白
soundtbugle 2008-06-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ccssddnnhelp 的回复:]
不难啊,你用的什么数据库? oracle , mysql, ms sql server 200 ?.

==== 思想重于技巧 ====


.
贴子分数 <20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.

reply via CSDN viewer
[/Quote]
mysql
soundtbugle 2008-06-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kaiyong 的回复:]
得说明t1表跟t2表的关系?
[/Quote]
你没看仔细吧!我要把t1的重复数里用t1.column1=t2.column1写到t2
soundtbugle 2008-06-20
  • 打赏
  • 举报
回复
连上面的例子都看不明白?很明显t1.column1=t2.column1
  • 打赏
  • 举报
回复
呵呵,这个应该如我17楼的说明
如果不是像我17楼所说的,那就不符合了
jhwcd 2008-06-20
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 xiaofanku 的回复:]
我作了个类4的测试,没问题.
try:
update t2 set total=(
select count(*)
from t1
where t1.column1 = t2.column1)

[/Quote]
这样写应该可以达到你的要求了!
街头小贩 2008-06-20
  • 打赏
  • 举报
回复
我作了个类4的测试,没问题.
try:
update t2 set total=(
select count(*)
from t1
where t1.column1 = t2.column1)
  • 打赏
  • 举报
回复
update t2 set total=(select count(*) from t1 where t1.column1 = t2.column1) 

这样就可以了
  • 打赏
  • 举报
回复
update t2 set total=(select count(column1) from t1 where t1.column1 = t2.column1)
这样就可以了
  • 打赏
  • 举报
回复
晕,那你举的例了我就比较迷糊了

你的意思是不是这样
t1.column1有重复的记录,例如
column1 column2
-------------------
2 1
2 4
1 1
5 10
6 11
5 12

把t1里的统计放到t2里更新????
而T2原来是:
column total
--------------------
2 0
1 0
5 0
6 0
这样的?是不是
你是想把T2更新成
t2.total记录重复的数量,例如
column1 total
--------------------
2 2
1 1
5 2
6 1
这样,对吧??
如果是就很简单了
soundtbugle 2008-06-20
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 angeldgz 的回复:]
SQL code
--查询
select column1,count(1) total from t1 group by column1
--更新
update t2 set total=(select count(1) from t1 where t1.column1 = t2.column1)
--插入
insert into t2 select column1,count(1) total from t1 group by column1
[/Quote]
需要在一条SQL语句中作到
soundtbugle 2008-06-20
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 iscandy 的回复:]
select column1 as column,count(column1) as total into t2 from t1 group by column1
[/Quote]
要更新到t2表中,你写的我也会
soundtbugle 2008-06-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chinmo 的回复:]
引用 8 楼 soundtbugle 的回复:
引用 5 楼 kaiyong 的回复:
得说明t1表跟t2表的关系?

你没看仔细吧!我要把t1的重复数里用t1.column1=t2.column1写到t2


那为什么变成0了呢???

现在不明白
[/Quote]
不用管为什么是0,需求就是把0更新成t1表统计出的数量!
angeldgz 2008-06-20
  • 打赏
  • 举报
回复

--查询
select column1,count(1) total from t1 group by column1
--更新
update t2 set total=(select count(1) from t1 where t1.column1 = t2.column1)
--插入
insert into t2 select column1,count(1) total from t1 group by column1
jhwcd 2008-06-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kaiyong 的回复:]
得说明t1表跟t2表的关系?
[/Quote]
嗯,同意。
不过我感觉一个t1表应该就可以重复的数据统计出来吧。
Sky_Wuhan 2008-06-19
  • 打赏
  • 举报
回复
得说明t1表跟t2表的关系?
gamehome007 2008-06-19
  • 打赏
  • 举报
回复
到底是几张表?
t1跟t2?
最后的total怎么还是在t2?
描述再清楚点...
ccssddnnhelp 2008-06-19
  • 打赏
  • 举报
回复

不难啊,你用的什么数据库? oracle , mysql, ms sql server 200 ?.
==== ====

.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
加载更多回复(2)

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧