查询的疑问

summer16md 2011-07-02 11:27:06
表里面有三个字段
id name count

要求是 当name的名字相同的话 查询出来count里面就增加1

比如
id name count
1 华硕 1
2 华硕 1
3 华硕 1
4 技嘉 1
5 微星 1

查出来 让华硕的count显示成3 其他不变 请问怎么查?
...全文
54 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
ly745455 2011-07-02
  • 打赏
  • 举报
回复
MARK
summer16md 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 acherat 的回复:]

SQL code

create table tb(id int,name nvarchar(10),[count] int)
insert into tb select 1,'华硕',1
insert into tb select 2,'华硕',1
insert into tb select 3,'华硕',1
insert into tb select 4,'技嘉',1
insert int……
[/Quote]

3Q 等下给你分啊 我出去吃下饭
AcHerat 2011-07-02
  • 打赏
  • 举报
回复

create table tb(id int,name nvarchar(10),[count] int)
insert into tb select 1,'华硕',1
insert into tb select 2,'华硕',1
insert into tb select 3,'华硕',1
insert into tb select 4,'技嘉',1
insert into tb select 5,'微星',1
go

select id=identity(int,1,1),name,count(*) as cnt
into #temp
from tb
group by name

select * from #temp

drop table #temp


drop table tb




/************
id name cnt
----------- ---------- -----------
1 华硕 3
2 技嘉 1
3 微星 1

(3 行受影响)

summer16md 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 qianjin036a 的回复:]

是要这样的结果吗?
SQL code
create table tb(id int,name nvarchar(10),[count] int)
insert into tb select 1,'华硕',1
insert into tb select 2,'华硕',1
insert into tb select 3,'华硕',1
insert into tb select 4,'技嘉',1
……
[/Quote]

不是 是要
1 华硕 3
2 技嘉 1
3 微星 1
这样的结果
cd731107 2011-07-02
  • 打赏
  • 举报
回复
select max(id),name,count(*)count
from tb group by name
-晴天 2011-07-02
  • 打赏
  • 举报
回复
是要这样的结果吗?
create table tb(id int,name nvarchar(10),[count] int)
insert into tb select 1,'华硕',1
insert into tb select 2,'华硕',1
insert into tb select 3,'华硕',1
insert into tb select 4,'技嘉',1
insert into tb select 5,'微星',1
go
select a.id,a.name,b.count from tb a inner join(
select name,count(*) count from tb group by name
)b on a.name=b.name
/*
id name count
----------- ---------- -----------
1 华硕 3
2 华硕 3
3 华硕 3
4 技嘉 1
5 微星 1

(5 行受影响)

*/
select a.id,a.name,(select count(*) from tb where name = a.name)ct from tb a
/*id name ct
----------- ---------- -----------
1 华硕 3
2 华硕 3
3 华硕 3
4 技嘉 1
5 微星 1

(5 行受影响)
*/
go
drop table tb
AcHerat 2011-07-02
  • 打赏
  • 举报
回复

select id=identity(int,1,1),name,count(*) as cnt
into #temp
from tb
group by name

select * from #temp

drop table #temp
summer16md 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 acherat 的回复:]

被楼主忽悠了。。。

SQL code

select id=row_number() over (order by getdate()),name,count(*) as cnt
from tb
group by name
[/Quote]

'row_number' 不是可以识别的 函数名。
AcHerat 2011-07-02
  • 打赏
  • 举报
回复
被楼主忽悠了。。。


select id=row_number() over (order by getdate()),name,count(*) as cnt
from tb
group by name
summer16md 2011-07-02
  • 打赏
  • 举报
回复
不知道呀 没遇到过
cd731107 2011-07-02
  • 打赏
  • 举报
回复
这种问题,楼主应该知道的吧
cd731107 2011-07-02
  • 打赏
  • 举报
回复
select id,name,count(*)count
from tb group by name
summer16md 2011-07-02
  • 打赏
  • 举报
回复
查出来还是有点问题
他查出来就变成了

id name count
1 华硕 3
2 华硕 3
3 华硕 3
4 技嘉 1
5 微星 1

我想让他显示成
1 华硕 3
2 技嘉 1
3 微星 1
AcHerat 2011-07-02
  • 打赏
  • 举报
回复
1楼是更新了count,查看表就知道了。
这是查询。

select a.id,a.name,(select count(*) from tb where name = a.name) cnt
from tb a
summer16md 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 acherat 的回复:]

SQL code

update t
set t.count = (select count(*) from tb where name = t.name)
from tb t
[/Quote]

没有显示结果呀
cd731107 2011-07-02
  • 打赏
  • 举报
回复
select a.id,a.name,(select count(*) from tb b where b.name = a.name)count 
from tb a
快溜 2011-07-02
  • 打赏
  • 举报
回复
select id,name,[count]=(select count(1) from tb where name=a.name)
from tb a
-晴天 2011-07-02
  • 打赏
  • 举报
回复
这样也行,但效率差点:
select a.id,a.name,(select count(*) from tb where name = a.name) from tb a
-晴天 2011-07-02
  • 打赏
  • 举报
回复
select a.id,a.name,b.count from tb a inner join(
select name,count(*) count from tb group by name
)b on a.name=b.name
AcHerat 2011-07-02
  • 打赏
  • 举报
回复

update t
set t.count = (select count(*) from tb where name = t.name)
from tb t
1,项目功能: (1)用户管理 普通用户管理  由管理员对普通用户的数据信息进行维护处理 系统用户管理  查询并展示系统用户的数据信息可以对数据信息进行增删改查的操作通过关键字可以查询出匹配的数据信息。 个人中心 登录成功的用户可以对个人信息进行修改(2)客房管理 客房列表 可以展示出客房的所有信息方便用户的查询。点击进入可以查看详细内容。并 能够通    过户型,价格区间等信息进行查询定位。 客房信息维护  管理员对客房的信息进行增删改查的操作。更改客房的信息。(3)入住管理 入住登记 用户进行入住登记,并且记录入住信息,如果用户是线下的则需要进行支付,如果是线上支付过的只需出示支付信息即可完成入住的登记。  入住信息查看 查询入住人员的信息可以根据身份证号或者房间号等信息进行用户信息的查询展示。  换房退房:此功能主要是进行退房和换房的操作。用户可以换房,或者是退房操作。 (4)留言管理  留言信息展示 普通用户登录系统后可以在留言区进行留言交流  留言内容回复 管理员对相关的疑问信息可以进行解答回复  留言信息管理 管理员通过列表形式进行留言信息的展示,对于一些偏激的言论可以不予展示处理。(5)新闻公告管理   新闻公告新增 管理员可以对新闻和公告的信息进行录入,录入后的数据用户在前台页面中将进行展示   新闻公告维护 主要是对新闻公告信息进行维护。   新闻公告信息列表展示 普通用户进入前台页面中将能看到新闻和公告信息。并能够点击查看相关内容。(6)订单管理   订单查询  根据单号日期等信息进行多角度的订单查询展示。   订单信息维护 更新订单状态以及联系方式等信息    我的订单  查询并展示订单列表信息。将订单列表以及订单的付款状态展示出来对于未付款的订单可以进行结算支付处理。(7)个人中心   用户注册 普通用户可以在前台进行自主注册,对于相同的身份证号只能有一个姓名与之对应进行ajax 的验证。   登录功能 不同的用户拥有不同的角色,在登录后用户能看到的功能也不相同。     适合做毕业设计参考项目。2,涉及技术:SSM框架,Tomcat3,开发环境:IDEA,MySQL数据库4,讲解方式:从环境安装,项目搭建,以及项目介绍等进行讲解5,包含资料:项目源码(含数据库文件),环境安装包,项目文档。

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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