开发者

returning a new row for each record

in a previous question I asked how to return id's from 3 different tables if the user_id was matched to a record in each o开发者_C百科f these tables. the sql-

select
    (select id from bla_facebook_accts where user_id = user.user_id) as facebook,
    (select id from bla_linked_in_accts where user_id = user.user_id) as linkedin,
    (select id from bla_twitter_accts where user_id = user.user_id) as twitter
from
    (select 12 user_id) user

Works great, but I would like each the select statements that return a whether or not each of the tables has a record to return a new row, and return multiple rows if a table has more than one record for each .


Here you go. This will also return the source table of the information.

SELECT'facebook' source, id FROM bla_facebook_accts WHERE (user_id = 12)
UNION
SELECT 'linkedin' source, id FROM bla_linked_in_accts WHERE (user_id = 12)
UNION
SELECT'twitter' source, id FROM bla_twitter_accts WHERE (user_id = 12) 


I believe you want to use a UNION construct:

select id from bla_facebook_accts where user_id = 12
UNION 
select id from bla_linked_in_accts where user_id = 12
UNION 
select id from bla_twitter_accts where user_id = 12
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜