开发者

Queries separated by union

I have 4 tables, Table a, Table b and Table c have same field names but Table d is having different field names, but I need to join the queries and have to be exported as a CSV file.

       select a开发者_如何学C.name,a.age,b.addr from a,b where a.rid=b.tid UNION select 
       a.name,a.age,c.addr from a,c where a.uiroll=c.piroll

I need to join table d also , but it has some fields and what I require are d.group and d.project and they are related to table a as d.uon=a.von

The result in cvs file should be as

     name     age     addr    group    project

and also the values.


Did you try something like :

SELECT a.name, a.age, b.addr, d.group, d.project 
FROM a, b, d 
WHERE a.rid=b.tib 
AND d.uon=a.von 
UNION 
SELECT a.name, a.age, c.addr, d.group, d.project 
FROM a, c, d 
WHERE a.uiroll=c.piroll 
AND d.uon=a.von


select a.name, a.age, b.addr, d.group, d.project 
from a
 LEFT JOIN b 
on a.rid = b.tid
 LEFT JOIN c
on a.uiroll = c.piroll
 LEFT JOIN d
on a.von = d.uon;


SELECT a.name,a.age,b.addr, d.group. d.project
FROM a
INNER JOIN b ON a.rid=b.tid
INNER JOIN d ON a.von=d.uon
UNION
SELECT a.name,a.age,c.addr, d.group. d.project
FROM a
INNER JOIN d ON a.von=d.uon
INNER JOIN c ON a.uiroll=c.piroll

Your description wasn't quite clear, but I guess this is what you want. You should also have a look at the JOIN syntax. Joins are essential when working with relational databases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜