在previous question中我询问如果user_id与每个表中的记录匹配,如何从3个不同的表返回id。 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
效果很好,但是我希望每个select语句返回一个表是否有一个记录来返回一个新行,如果一个表有多个记录,则返回多行。
答案 0 :(得分:2)
你走了。这也将返回信息的源表。
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)
答案 1 :(得分:1)
我相信您想要使用UNION
构造:
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