[英]Python or Excel: How can you compare 2 columns and then write the value of a 3rd column in a new column?
I have 2 tables that look like this:我有 2 个看起来像这样的表:
Table 1表格1
| ID | Tel. | Name |
|:--:|:----:|:-------:|
| 1 | 1234 | Denis |
| 2 | 4567 | Michael |
| 3 | 3425 | Peter |
| 4 | 3242 | Mary |
Table 2表 2
| ID | Contact Date |
|:--:|:------------:|
| 1 | 2014-05-01 |
| 2 | 2003-01-05 |
| 3 | 2020-01-10 |
| 4 | NULL |
Now I want to Compare the First Table with the second table with the ID
column to look if contact 1
is already in the list where people were contacted.现在我想比较第一个表和带有ID
列的第二个表,以查看contact 1
是否已经在contact 1
列表中。 After this, I want to write the Contact Date
into the first table to see the last contact date in the main table.在此之后,我想将Contact Date
写入第一个表中以查看主表中的最后一个联系日期。
How would I do this?我该怎么做?
Thanks for any answers!!感谢您的任何答案!
Here's a solution that might get your interest using MySQL ;这是一个使用MySQL可能会引起您兴趣的解决方案; Then if you want to implement it into Python will be easy.那么如果你想将它实现到Python 中会很容易。
First, Let's create our T1
Table AS CODED首先,让我们创建我们的T1
表AS CODED
create table T1(
id integer primary key,
tel integer,
name varchar(100)
);
create table T2(
id integer primary key,
contactDate date
);
Second Table T2
第二桌T2
create table T2(
id integer primary key,
contactDate date
);
Insertion Of T1
AND T2
: T1
和T2
插入:
-- Table 'T1' Insertion
insert into T1(id, tel, name) VALUES
(1, 1234, "Denis"),
(2, 4567, "Michael"),
(3, 3425,"Peter"),
(4, 3242, "Mary");
-- Table 'T2' Insertion
insert into T2(id, contactDate) VALUES
(1, 20140105),
(2, 20030105),
(3, 20201001),
(4, Null);
Then, Create T3
With the Select Statement for both tables using INNER JOIN
For Joins
Results然后,使用INNER JOIN
For Joins
Results 为两个表创建带有 Select 语句的T3
CREATE TABLE T3 AS
SELECT T1.id, T1.name, T1.tel, T2.contactDate
FROM T1
INNER JOIN T2 ON T1.id=T2.id;
Then, SELECT
to check the results然后, SELECT
检查结果
select * from T3;
OUTPUT输出
| id | name | tel | contactDate |
|:--:|:-------:|------|-------------|
| 1 | Denis | 1234 | 2014-01-05 |
| 2 | Michael | 4567 | 2003-01-05 |
| 3 | Peter | 3425 | 2020-10-01 |
| 4 | Mary | 3242 | NULL |
I Hope This Helps.我希望这有帮助。 I was really trying to merge the T3
---> contactDate
with T1
for like 3 hours.我真的想将T3
---> contactDate
与T1
合并 3 个小时。 But it was process heavy.但过程繁重。 I will attach Links that could help you more.我会附上可以帮助你更多的链接。
SQL ALTER TABLE Statement SQL ALTER TABLE 语句
SQL Server INSERT Multiple Rows SQL Server 插入多行
INSERT INTO SELECT statement overview and examples INSERT INTO SELECT 语句概述和示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.