select * from table1 where ID1 in (select id from table3 where Tag=0 and UserID='A') union select * from table2 where ID2 in (select id from table3 where Tag=1 and UserID='A')
晕! 楼主写了那么多....想复杂了吧....union不就得了?
小弟是刚接触SQL Server,还是门外汉,诸位见笑! 请问下面两种方法那种效率高,还是一样?select * from table1 where ID1 in (select id from table3 where Tag=0 and UserID='A') union select * from table2 where ID2 in (select id from table3 where Tag=1 and UserID='A')------------------------------------------------------------------------------------ Select * From Table1,Table3 WHERE Table1.ID1=Table3.ID AND Table3.Tag=0 AND Table3.UserID='A' UNION ALL Select * From Table2,Table3 WHERE Table2.ID2=Table3.ID AND Table3.Tag=1 AND Table3.UserID='A' ------------------------------------------------------------------------------------ 补充问一点:我的表中Name为nText字段类型,单独用UNION则提示错误:不能以DISTINCT方式选择Text、nText或Image数据类型。用UNION ALL则正确。
使用 UNION 组合两个查询的结果集的两个基本规则是: 1:所有查询中的列数和列的顺序必须相同。 2:数据类型必须兼容UNION:指定组合多个结果集并将其作为单个结果集返回。 UNION ALL:在结果中包含所有的行,包括重复行。
union
select * from table2 where ID2 in (select id from table3 where Tag=1 and UserID='A')
请问下面两种方法那种效率高,还是一样?select * from table1 where ID1 in (select id from table3 where Tag=0 and UserID='A')
union
select * from table2 where ID2 in (select id from table3 where Tag=1 and UserID='A')------------------------------------------------------------------------------------
Select * From Table1,Table3 WHERE Table1.ID1=Table3.ID AND Table3.Tag=0 AND Table3.UserID='A'
UNION ALL
Select * From Table2,Table3 WHERE Table2.ID2=Table3.ID AND Table3.Tag=1 AND Table3.UserID='A'
------------------------------------------------------------------------------------
补充问一点:我的表中Name为nText字段类型,单独用UNION则提示错误:不能以DISTINCT方式选择Text、nText或Image数据类型。用UNION ALL则正确。
1:所有查询中的列数和列的顺序必须相同。
2:数据类型必须兼容UNION:指定组合多个结果集并将其作为单个结果集返回。
UNION ALL:在结果中包含所有的行,包括重复行。