当前位置: 代码迷 >> Sql Server >> 两个报表中条件查询
  详细解决方案

两个报表中条件查询

热度:16   发布时间:2016-04-24 10:24:48.0
两个表格中条件查询?
表格a:
NAME AGE
aa         18
bb         21
cc         22
dd         25
ff         19

表格b:
NAME MOBILE
aa 11111111     
aa 22222222     
dd 33333333     
dd 44444444     
gg 55555555     
jj 66666666     

现在的要求是,先查询出a表中年龄小于22的,然后再查询它在b表中相关所有电话号码,请问怎么查询,谢谢。

------解决方案--------------------
select a.*,b.*
from a inner join b  on a.NAME =b.NAME 
where a.AGE<22
------解决方案--------------------
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(發糞塗牆)
-- Date    :2014-07-08 12:50:35
-- Version:
--      Microsoft SQL Server 2012 - 11.0.5058.0 (X64) 
-- May 14 2014 18:34:29 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go 
create table [a]([NAME] varchar(2),[AGE] int)
insert [a]
select 'aa',18 union all
select 'bb',21 union all
select 'cc',22 union all
select 'dd',25 union all
select 'ff',19
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go 
create table [b]([NAME] varchar(2),[MOBILE] int)
insert [b]
select 'aa',11111111 union all
select 'aa',22222222 union all
select 'dd',33333333 union all
select 'dd',44444444 union all
select 'gg',55555555 union all
select 'jj',66666666
--------------开始查询--------------------------

SELECT  a.* ,
        b.*
FROM    a
        INNER JOIN b ON a.NAME = b.NAME
WHERE   a.AGE < 22
----------------结果----------------------------
/*
NAME AGE         NAME MOBILE
---- ----------- ---- -----------
aa   18          aa   11111111
aa   18          aa   22222222 
*/
  相关解决方案