简体   繁体   English

#Sql将一张表的数据与选择查询结果的数据进行比较

[英]#Sql Comparing data of one table to that of select query result

I have table 1 with A,B,C,D,E column and the result of select query after combining the table 2 and table 3 is A, D column(few values as compare to table 1) Say view 1. 我有带有A,B,C,D,E列的表1,将表2和表3合并后的选择查询结果是A,D列(与表1相比,值很少)说视图1。

Now I need to run another query if value of column A,D of table 1 is in resulted view. 现在,如果在结果视图中表1的A,D列的值,我需要运行另一个查询。 this new query also have where conditions and combining two tables 这个新查询还具有条件条件并结合了两个表

Could anyone please let me know the best way out? 有人可以让我知道最好的出路吗?

Example Table1 示例表1

A              B        C   D

Fruit       Orange      1   3

Vegetable   Onion      89   3

Fruit       Mango      83   22

Fruit       Banana      3   2

Vegetable   Beans     382   2

View1 查看1

A             B     
Fruit       Orange      
Fruit       Banana      
Vegetable   Beans   

Final output 最终输出

A              B      C  D

Fruit       Orange    1  3

Fruit       Banana    3  2

Vegetable   Beans   382  2
SELECT
    view1.a,
    view1.b,
    table1.c,
    table1.d
FROM
    table1
    JOIN (
    --select query of your view
        SELECT
            *
        FROM
            your_view
    ) view1 ON
        table1.a = view1.a
    AND
        table1.b = view1.b;

All you need is the inner join between your table and view. 您所需要做的就是表和视图之间的inner join Inner join will find the matches of column A & B in both tables. Inner join将在两个表中找到列A & B的匹配项。

  select table1.*
    from table 1
    inner join view1
    on table1.a = view1.a 
    and table1.b = view1.b;

Hope it helps! 希望能帮助到你!

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

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