简体   繁体   English

SQL查询以从表或表中选择数据

[英]SQL query to select data from either a table or from b table

I have 2 tables a and b. 我有2张桌子a和b。

Table a
I'd   name

Table b
I'd name address

Now I have one name which may be present in either in a table or in b table. 现在,我有一个名称可以出现在表中或b表中。 So I have to write a single to select the I'd of the person based on name in plsql. 因此,我必须编写一个代码,以便根据plsql中的名称选择此人的身份。

I think you can use UNION like this: 我认为您可以像这样使用UNION

SELECT *
FROM (
    SELECT Id, name, address, 'b' as tableName
    FROM b
    UNION ALL
    SELECT Id, name , '', 'a'
    FROM a) dt
WHERE name = yourName;

To have aa preview of the result: [SQL Fiddle] 要预览结果: [SQL Fiddle]

You also have name of the table in tableName column. 您还可以在tableName列中找到表的名称。

select name from table_a where id = <id>
union
select name from table_b where id = <id>

or 要么

select id from table_a where name = 'john'
union
select id from table_b where name = 'john'

This will get you the name from either table a or table b. 这将从表a或表b中获取名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM