SQL数据库多表联合查询
来源:百度知道 编辑:UC知道 时间:2024/07/04 17:49:58
请高手们帮帮忙
这是我自己写的视图 但是查询后有重复的数据 不晓得怎么筛选数据 高手们帮我改一下
create view _Ticket
as
select ID,Ticket from A union select ID,Ticket from B union select ID,Ticket from C
select * from _Ticket
以下6楼的SQL语句都没能去重复
....高手们继续
create view_ticket as
select id,ticket from a group by id,ticket union
select id,ticket from b group by id,ticket union
select id,ticket from c group by id,ticket
首先,在每句里去掉重复的,这样能保证三个查询中,每个子查询无重复
然后union 本身就是能自动去重,也就是如果a,b,c中三个查询中有互相重复的数据,会过滤掉
顺便解释一下,union all是不会去重的
你可以试试下面的sql
create or replace view view _Ticket as
select(
select ID,Ticket from a
union
select ID,Ticket from b
union
select ID,Ticket from c) d where d.ID=xxxx and d.Ticket=xxxx;
----
以上,希望对你有所帮助。
楼主,看我写的怎样
create view view_ticket
as
select distinct a.[id],a.ticket from
(
select [id],ticket from tb_a
union all
select [id],ticket from tb_b
union all
select [id],ticket from tb_c ) a
create view _Ticket
as
select DISTINCT ID,Ticket
from A
union
select DISTINCT ID,Ticket
from B
union
select DISTINCT ID,T