检查值是否存在于多个表中-Oracle SQL

ang

我有以下表格:

table_user_1
col6        member_records
123         5
456         6
222         4

table_user_2
col5        member_records
554         5
456         6
124         4

table_user_3
col7        member_records
123         5
755         6
449         4

我想查看所有用户联合,然后检查用户是否在表上。

对于工会,我尝试过:

select col6 from table_user_1
union
select col5 from table_user_2
union
select col7 from table_user_3;

输出应为:

member     table_user_1       table_user_2      table_user_3
123        1                  0                 1
456        1                  1                 0
222        1                  0                 0
554        0                  1                 0
124        1                  1                 0
755        1                  0                 1
449        1                  0                 1

如何生成上述输出?

akk0rd87
with table_user_1(col6, member_records) as (
select 123, 5 from dual union all
select 456, 6 from dual union all
select 222, 4 from dual),

table_user_2(col5, member_records) as (
select 554, 5 from dual union all
select 456, 6 from dual union all
select 124, 4 from dual),

table_user_3(col7, member_records) as (
select 123, 5 from dual union all
select 755, 6 from dual union all
select 449, 4 from dual)

select *
from
   (select col6, 1 as table_id from table_user_1
    union all
    select col5, 2 from table_user_2
    union all
    select col7, 3 from table_user_3
   )
pivot(count(*) for table_id in (1 as table_user_1, 2 table_user_2, 3 as table_user_3));

      COL6 TABLE_USER_1 TABLE_USER_2 TABLE_USER_3
---------- ------------ ------------ ------------
       123            1            0            1
       222            1            0            0
       554            0            1            0
       755            0            0            1
       456            1            1            0
       449            0            0            1
       124            0            1            0

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PL/SQL 检查输入的值是否存在于不同的表中

是否有任何SQL查询来检查值是否存在或不存在于数据库表中

如何在 Oracle 中编写一个简单的查询或 PL/SQL 代码来检查 order_id 是否存在于一个表中,然后再处理另一个表?

检查记录是否存在于SQL表中的最快方法

如何使用SQL检查一个表中存在的json值数组是否存在于另一个表中?

检查列值是否存在于SQL的另一列中

SQL Server和C#-如何检查我要上传的varbinary(max)文件是否已存在于表中?

PHP SQL 检查表单输入是否存在于其他表中,然后继续

我如何检查值是否存在于 sql server 中指定的日期之前

我想编写一个 sql 查询来获取存在于 oracle 表的 clob 列中的soap xml 中的两个标签之间的数据

如何检查SQL服务器中的所有记录,是否存在于MySQL中?

Oracle PL/SQL:如何有条件地使用列,因为它可能不存在于旧版本的表中

检查节点是否存在于 SQL Server xml 列中

T-SQL检查表是否存在于架构中

如何检查记录是否已存在于SQL数据库中?

使用C#检查文本是否已存在于SQL Server中

如何检查条目是否存在于单独的SQL Server表中,以及如何根据所存在的功能更改链接的功能

如何验证输入的发票号是否已经存在于我的sql表中?

SQL Server-是否将返回的行值全部存在于集合中

如果存在于另一个表 sql 中,则标记列的值

SQL查询检查值基于另一个列值存在于查找中

使用 asp.net 时,如何检查我的 pin 是否存在于 SQL 数据库中?

SQL-如何根据用户是否存在于另一个表中来选择该用户?

SQL选择*,但是如果存在于另一个表中,则覆盖行的“列值”

SQL 存在于 TableA 或 TableB 中

Oracle SQL中找出表中是否存在一个或多个重复项的最快方法是什么?

SQL:检查多个表中是否存在多条记录

如果列存在于具有条件的其他表中,则 Oracle 计数

Oracle文本:CONTAINS不返回任何值,即使它们存在于索引中