这个SQL语句怎样写

qkyh19731003 2012-09-12 03:00:38
人员表:
工号 姓名 性别
Gh001 张三 男
Gh002 李四 女
Gh003 王五 男
Gh004 Alice 女

工资表:
工号 月份 工资
Gh001 一月份 1500
Gh002 一月份 1800
Gh002 一月份 1300
Gh003 一月份 2000

我想查询:
1.女性工资在1500以上的人员
2.统计女性工资在1500以上的人员的个数

请专家指教!
...全文
176 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
以学习为目的 2012-09-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

to wr4719:
根据您的语句,假如我这样写:
select * from 人员表 ,工资表 where 人员表.性别='女'and 工资表.工资>=1500 and 人员表.工号=工资表.工号

则必须添加"and 人员表.工号=工资表.工号",否则有重复,结果正确,但却出现了两个“工号”字段,怎么办?
[/Quote]试试2楼的
qkwre68 2012-09-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
to wr4719:
根据您的语句,假如我这样写:
select * from 人员表 ,工资表 where 人员表.性别='女'and 工资表.工资>=1500 and 人员表.工号=工资表.工号

则必须添加"and 人员表.工号=工资表.工号",否则有重复,结果正确,但却出现了两个“工号”字段,怎么办?
[/Quote]
指定工号字段隶属于哪个表,可以这样写
select 人员表.工号 from 人员表 ,工资表 where 人员表.性别='女'and 工资表.工资>=1500 and 人员表.工号=工资表.工号
酸甜桔子 2012-09-12
  • 打赏
  • 举报
回复
select 姓名, count(姓名) from 人员表 a , 工资表 b
where a.工号 = b.工号 and a.性别 = '女' and b.工资 > 1500
holicc 2012-09-12
  • 打赏
  • 举报
回复
你把select * from 改为select 人员表.* ,工资表.工资 from
不就没有两个工号了嘛
qkyh19731003 2012-09-12
  • 打赏
  • 举报
回复
to wr4719:
根据您的语句,假如我这样写:
select * from 人员表 ,工资表 where 人员表.性别='女'and 工资表.工资>=1500 and 人员表.工号=工资表.工号

则必须添加"and 人员表.工号=工资表.工号",否则有重复,结果正确,但却出现了两个“工号”字段,怎么办?
qkyh19731003 2012-09-12
  • 打赏
  • 举报
回复
to fengqi1314:
第一问成功,第二问应改:select count(*)as 人数 from 人员表 a , 工资表 b
where a.性别 = '女' and b.工资 > 1500 and a.工号 = b.工号

其它兄弟的在设计视图中运行均有问题!
gua84607583 2012-09-12
  • 打赏
  • 举报
回复

if object_id('[People]') is not null drop table [People]
go
if OBJECT_ID('[Sale]')is not null drop table [Sale]
go
create table [People]([No] varchar(6),[Name] varchar(6),[Sex] varchar(2))
go
create table [Sale]([No] varchar(6),[SMonth] varchar(6),[SSale] int)
go
insert [People]
select 'Gh001','张三','男' union all
select 'Gh002','李四','女' union all
select 'Gh003','王五','男' union all
select 'Gh004','Alice','女'
go
insert [Sale]
select 'Gh001','一月份',1500 union all
select 'Gh002','一月份',1800 union all
select 'Gh002','一月份',1300 union all
select 'Gh003','一月份',2000
go
--女性工资在1500以上的人员--
with TotalSale as
(
select No,SMonth,SUM(SSale) as [totalSale] from Sale group by No,SMonth
)
select p.No,p.Name,p.Sex,t.SMonth,t.totalSale from People p left join TotalSale t on p.No = t.No where p.Sex='女' and t.totalSale>1500;
--统计女性工资在1500以上的人员的个数--
with TotalSale as
(
select No,SMonth,SUM(SSale) as [totalSale] from Sale group by No,SMonth
)
select COUNT(1) as [人数] from People p left join TotalSale t on p.No = t.No where p.Sex='女' and t.totalSale>1500;
wr4719 2012-09-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code
select name,count(name) from employee ,salares where employee.sex='女'and salares.salare>=1500
[/Quote]
select name,count(name) from employee ,salares where employee.sex='女'and salares.salary>=1500
老白猫 2012-09-12
  • 打赏
  • 举报
回复

select a.* from 人员表 a , 工资表 b
where b.工资 > 1500 and a.性别 = '女' and a.工号 = b.工号

select count(a.*) from 人员表 a , 工资表 b
where b.工资 > 1500 and a.性别 = '女' and a.工号 = b.工号
以学习为目的 2012-09-12
  • 打赏
  • 举报
回复
1、
select name from employee a left join salary b on a.emp_no=b.emp_no where a.sex_code='女'and salary>'1500'
2、
select COUNT(*) from employee a left join salary b on a.emp_no=b.emp_no where a.sex_code='女'and salary>'1500'
wr4719 2012-09-12
  • 打赏
  • 举报
回复
select name,count(name) from employee where sex='女'and salares>=1500

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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