如何在 SQL 中比较两个不同表中的列

原文:https://www . geesforgeks . org/如何比较 sql 中两个不同表中的列/

这里我们将看到如何在 SQL 中比较两个不同表的列。我们将举几个例子来看看我们如何以不同的方式做到这一点。

概述: 在此,我们将了解执行所需操作的 SQL 查询概述如何在 SQL 中比较两个不同表中的列。我们将借助例子来理解每个概念。

步骤-1:创建数据库: 要创建数据库,请使用如下 SQL 查询。

语法–

create database_name;

示例–

create STUDENTS_DATA

第 2 步:使用数据库: 如下使用该数据库。

语法–

use database_name;

示例–

use STUDENT_DATA

步骤 3:创建表: 创建用于进行查询的表,如下所示。

表-1:语法–

create table table_name
(
column1 type, 
column2 type, 
... 
);

示例–

create table studentData1 
(
roll_number int primary key, 
firstname varchar(100), 
lastname varchar(100), 
marks int
);

第 4 步:插入记录: 在表格 studentData1 中插入记录,如下所示。 语法–

insert into table_name(column1, column2 ...) values (value1, value2 ...);

插入学生数据 1 表–

insert into studentData1 (roll_number, firstname, lastname, marks) 
values (1, 'albert', 'einstein',356);
insert into studentData1 (roll_number, firstname, lastname, marks) 
values (2, 'isaac', 'newton',412);
insert into studentData1 (roll_number, firstname, lastname, marks) 
values (3, 'marie', 'curie',436);
insert into studentData1 (roll_number, firstname, lastname, marks) 
values (4, 'philip', 'jsam',389);
insert into studentData1 (roll_number, firstname, lastname, marks) 
values (5, 'tom', 'jsam',452);
insert into studentData1 (roll_number, firstname, lastname, marks) 
values (6, 'tucker', 'jose',412);
insert into studentData1 (roll_number, firstname, lastname, marks) 
values (7, 'drawn', 'caste',389);

第 5 步:创建表 2 : 这里,我们将使用 SQL 查询创建 studentData2,如下所示。

create table studentData2 
(
id int primary key, 
firstname varchar(100), 
lastname varchar(100), 
marks int
);

第 6 步:插入记录: 在表格 studentData2 中插入记录如下。

insert into studentData2 (id, firstname, lastname, marks) 
values (2, 'isaac', 'newton',412);
insert into studentData2 (id, firstname, lastname, marks) 
values (3, 'marie', 'curie',436);
insert into studentData2 (id, firstname, lastname, marks) 
values (6, 'tucker', 'jose',412);
insert into studentData2 (id, firstname, lastname, marks) 
values (4, 'philip', 'jsam',389);

示例-1 : 使用 where 子句比较两个不同表的列。它不能处理空值。 语法: (选择所有列的所有记录)

select * from table1, table 
where 
table1.col1 = table2.col2 and table1.col1 > someValue;

语法(可选): (从表中选择特定列)

select t1.col1, t2.col2,t3\. col3 ...., t2.col1, t2.col2, t2.col3....  
from table1 t1, table t2 where t1.col1 = t2.col2 and t1.col1 <> t2.col2;

查询–

select * from studentData1, studentData2 
where studentData1.roll_number = studentData2.id;

输出: 基于 roll_number 和 id 比较的结果如下。

T20T23【艾萨克】T24T27】412T29】2T31T33】3T35】玛丽【T36 | Volume _ number | name | surname | mark | 身份证明(identification) | | --- | --- | --- | --- | --- | | Two | newton |

示例-2 : 使用联接在表中按优先级比较列。例如,左连接返回第一个表中的所有值,对于第二个表中不匹配的记录返回空值。同样,我们可以根据自己的需要使用右连接、内部连接、完全连接和自连接。在下面的例子中,我们使用左连接比较了基于 roll_number 和 id 列的两个表。

语法–

select t1.col1, t1.col2... , t2.col1, t2.col2... , 
from table1 as t1 left 
join table2 as t2 on 
tabe1.col1 = table2.col1;

查询–

select a.roll_number, a.firstname, b.id 
from studentData1 as a left 
join 
studentData2 as b on 
a.roll_number = b.id;

输出: 基于左表的连接,即如下所示的 sutdentData1。

T21T25】2T29】2T33T35】玛丽 T37】3【T37 | Volume _ number | name | 身份证明(identification) | | --- | --- | --- | | one | Albert | | isaac |

示例-3 : UNION 允许我们比较两种相同类型的表或数据集。一旦我们可以拥有两个表的联合,我们就可以使用联合来比较列。它可以快速检查任一表中有哪些数据丢失或更改。它能够处理 where 子句无法处理的空值。

注意– 只有当我们有相同类型的表时,才使用这个。

语法–

select col1, col2, col3 ....  
from (select col1, col2, col3 ... 
from Table1 union all select col1, col2, col3 ... 
from Table2) 
cmpr order by ID;

示例–

select * from 
(select * from studentData1 
where roll_number > 4 
union all 
select * from studentData2 where id < 6) 
cmpr order by marks;

输出: 对比相同的记录,其中 roll_number > 4 和 id < 6 如下。

T28T31】菲利普 T33】jsamt385 | Volume _ number | name | surname | mark | | --- | --- | --- | --- | | seven | draw | 他作弊了 | Three hundred and eighty-nine | | four |