数据为主子表结构返回的结果集是主表中的数据字段
条件是 子表中某个字段的值等于参数 的主表记录例如:
主表
name age sex
chen 20 F
wang 21 W子表
name class
chen chn
chen eng
wang chn如果输入查询条件:chn ,则返回主表的 两条记录,如果查询条件为 eng ,则只返回主表的 chen那条记录谢谢!
条件是 子表中某个字段的值等于参数 的主表记录例如:
主表
name age sex
chen 20 F
wang 21 W子表
name class
chen chn
chen eng
wang chn如果输入查询条件:chn ,则返回主表的 两条记录,如果查询条件为 eng ,则只返回主表的 chen那条记录谢谢!
解决方案 »
- sql 复杂查询的问题
- 想写一个F_SplitCharNum的自定义函数
- 关于 IDENTITY 的问题,以前没人问过,很难。
- 求救!在查询分析中使用net命令问题
- 怎样查出这样的数据??、有点难度,想了好久,没好的思路..
- 棘手问题
- [馨郁星愿]企业管理器SQL server组中为什么显示无项目,急救sos
- 打不开"查询分析器"是什么原因呢?
- 请教一句sql语句 select * from database1 where id in (select id from 这里是个datatable怎么写呢)
- 请问sql server 在批量插入数据{insert into table values(),(),()}的时候,有没办法解决重复问题?
- SQL帐号加权限.
- 转换
set @class='eng'
select * from [主表] a join [子表] b on a.name=b.name where b.[class]=@class
??
(
select name from 子表 where class ='chn'
)
from 主表
where
name in(select name from 子表 where class='chn')
select a.* from 主表 a ,子表 b
where a.name=b.name and b.class='chn'
where exists(select 1 from 子表 where name=a.name and class='参数')
and class='eng'--可以等于你要查询的
--> 测试数据:[主表]
if object_id('[主表]') is not null drop table [主表]
create table [主表]([name] varchar(4),[age] int,[sex] varchar(1))
insert [主表]
select 'chen',20,'F' union all
select 'wang',21,'W'select * from [主表]
--> 测试数据:[子表]
if object_id('[子表]') is not null drop table [子表]
create table [子表]([name] varchar(4),[class] varchar(3))
insert [子表]
select 'chen','chn' union all
select 'chen','eng' union all
select 'wang','chn'select a.* from [主表] a
join [子表] b on a.name=b.name
where b.class='chn'
/*
name age sex
---- ----------- ----
chen 20 F
wang 21 W(2 行受影响)*/
from 主表
where
name in(select name from 子表 where class='chn')
*
from
主表
where
name
in
(select name from 子表 where class='chn')