SQL查询语句的一个问题!

uhoo 2007-12-17 05:32:31
姓名 籍贯 年龄
张三 山东 23
王五 山东 27
李四 河北 23
陈六 河北 27




能否用一句SQL 将山东和河北年龄最小的查询出来?
结果如下:

姓名 籍贯 年龄
张三 山东 23
李四 河北 23
...全文
342 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhujinqiang 2007-12-19
  • 打赏
  • 举报
回复
select A.*
from table A
inner join
(select 籍贯,min(年龄) as 年龄m
from table group by 籍贯 having 籍贯 in('河北','山东')) B
on A.籍贯=B.籍贯 and A.年龄=B.年龄m
晓风残月0110 2007-12-19
  • 打赏
  • 举报
回复

declare @t table (name varchar(20),address varchar(20),age int)
insert into @t select '张三','山东',23
insert into @t select '王五','山东',27
insert into @t select '李四','河北',23
insert into @t select '陈六','河北',27

--第二种
select * from @t a where not exists(select 1 from @t where address=a.address and age <a.age)
--第三种
select a.* from @t a left join (select address,min(age) as age from @t group by address) b
on a.address = b.address where b.age=a.age
/*name address age
-------------------- -------------------- -----------
张三 山东 23
李四 河北 23
晓风残月0110 2007-12-19
  • 打赏
  • 举报
回复

--年龄最大
select * from @t a where age = (select max(age) from @t where address=a.address)
--顺序去第一条
select * from @t a where age = (select top 1 from @t where address=a.address )
--随机去第一条
select * from @t a where age = (select top 1 from @t where address=a.address order by newid())
晓风残月0110 2007-12-19
  • 打赏
  • 举报
回复
playwarcraft怎么了


declare @t table (name varchar(20),address varchar(20),age int)
insert into @t select '张三','山东',23
insert into @t select '王五','山东',27
insert into @t select '李四','河北',23
insert into @t select '陈六','河北',27

select * from @t a where age = (select min(age) from @t where address=a.address) order by name
---
/*name address age
-------------------- -------------------- -----------
李四 河北 23
张三 山东 23
toury 2007-12-18
  • 打赏
  • 举报
回复

<%
SELECT TABLENAME.姓名,T1.籍贯,t1.A FROM TABLENAME,(SELECT 籍贯,MIN(TABLENAME.年龄) AS A FROM TABLENAME GROUP BY TABLENAME.籍贯) AS T1 WHERE TABLENAME.籍贯=T1.籍贯 AND TABLENAME.年龄=t1.A
%>

---------------
数据:
id 姓名 籍贯 年龄
1 张三 山东 23
2 王五 山东 27
3 李四 河北 24
4 陈六 河北 27

结果:
姓名 籍贯 A
张三 山东 23
李四 河北 24
fuyu131 2007-12-18
  • 打赏
  • 举报
回复


Select 姓名,籍贯,MIN(年龄)As 年龄 from table where 籍贯='山东'OR 籍贯='河北' group by 籍贯
playwarcraft 2007-12-18
  • 打赏
  • 举报
回复


select A.*
from T A
inner join
(select 籍贯,min(年龄) as 年龄
from T group by 籍贯) B
on A.籍贯=B.籍贯 and A.年龄=B.年龄


goodbetter_4107607 2007-12-18
  • 打赏
  • 举报
回复
select * from Yourtable where [年龄] in (select min([年龄]) from Yourtable group by [籍贯])
应该可以了。
littlelam 2007-12-18
  • 打赏
  • 举报
回复
如果是oracle 有rowid就很容易了

好像csdn上有讨论过这样的题目。。。
js_mi 2007-12-18
  • 打赏
  • 举报
回复
这位兄弟grellen 的应该可以.把UNION改成UNION ALL 应该更好.

select * from 表名 where 籍贯='山东' and 年龄=(select min(年龄) from 表名 where 籍贯='山东')
union all
select * from 表名 where 籍贯='河北' and 年龄=(select min( 年龄) from 表名 where 籍贯='河北')
半山闲人 2007-12-18
  • 打赏
  • 举报
回复
SELECT top 1 * FROM 表1 where 籍贯="山东" order by 年龄 union SELECT top 1 * FROM 表1 where 籍贯="河北" order by 年龄
测试通过,不过只能返回一个籍贯的一条记录,如果有多条,就不是一句话可以搞定了
hjh5303 2007-12-18
  • 打赏
  • 举报
回复
select * from table where 年龄 in (select min(年龄) from test group by 籍贯)
grellen 2007-12-17
  • 打赏
  • 举报
回复
select * from 表名 where 籍贯='山东' and 年龄=(select min(年龄) from 表名 where 籍贯='山东')
union
select * from 表名 where 籍贯='河北' and 年龄=(select min( 年龄) from 表名 where 籍贯='河北')
grellen 2007-12-17
  • 打赏
  • 举报
回复
这样应该就可以了吧!!!
grellen 2007-12-17
  • 打赏
  • 举报
回复
select * from tb where 籍贯='山东' and 年龄=(select min(年龄) from 表名称 where 籍贯='山东')
union
select * from tb where 籍贯='河北' and 年龄=(select min(年龄) from 表名称 where 籍贯='河北')
Go 旅城通票 2007-12-17
  • 打赏
  • 举报
回复
--更正原来的代码,这个可以在mssql运行
select tempTB.* from
(
select * from 表名称 where 籍贯='山东' and 年龄=(select min(年龄) from 表名称 where 籍贯='山东')
union
select * from 表名称 where 籍贯='河北' and 年龄=(select min(年龄) from 表名称 where 籍贯='河北')
) as tempTB


--ACCESS
select * from
(
select * from tb where 籍贯='山东' and 年龄=(select min(年龄) from 表名称 where 籍贯='山东')
union
select * from tb where 籍贯='河北' and 年龄=(select min(年龄) from 表名称 where 籍贯='河北')
)
acong110 2007-12-17
  • 打赏
  • 举报
回复
select 姓名,籍贯,min(年龄) from table group by 籍贯
fcuandy 2007-12-17
  • 打赏
  • 举报
回复
呵呵,楼主分真多.
uhoo 2007-12-17
  • 打赏
  • 举报
回复
没有一个对的,目前!
Go 旅城通票 2007-12-17
  • 打赏
  • 举报
回复
3楼的可以全部选出
加载更多回复(6)

28,391

社区成员

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

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