数据库查询条件问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 12:51:55
我用select id,name,sex,school from t1
union
select id,name,sex,school from t2
union
select id,name,sex,school from t3
union
select id,name,sex,school from t4
语句对t1,t2,t3,t4这4个表进行查询 后面却不能使用where 语句比如在上面的代码后加 where name like '%aa'这句查询结果还是4个表的所有数据,请问有结局办法吗 急急急
select id,name,sex,school from
(
select id,name,sex,school from t1
union
select id,name,sex,school from t2
union
select id,name,sex,school from t3
union
select id,name,sex,school from t4
)where name like '%aa'
这种方法提示where附近出错。。。。。。

先把从四个表查询出来的结果生成到一张新的表中,然后再用where语句来查询

select id,name,sex,school from
(
select id,name,sex,school from t1
union
select id,name,sex,school from t2
union
select id,name,sex,school from t3
union
select id,name,sex,school from t4
)where name like '%aa'

2搂正解,要用子查询

select id,name,sex,school from t1 where name like '%aa'
union all
select id,name,sex,school from t2 where name like '%aa'
union all
select id,name,sex,school from t3 where name like '%aa'
union all
select id,name,sex,school from t4 where name like '%aa'