用Python连接表
我需要在Python中复制下面的SQL查询,并在Python中获得结果用Python连接表,python,sql,Python,Sql,我需要在Python中复制下面的SQL查询,并在Python中获得结果 select a.customer_id as custid_tab1, a.country_id as ctryid_tab1, b.customer_id_tab2 as custid_tab2, b.country_id_tab2 as ctryid_tab2, c.customer_id_tab3 as custid_tab3, c.country_id_tab3 as ctryid_tab3 from
select a.customer_id as custid_tab1, a.country_id as ctryid_tab1, b.customer_id_tab2 as custid_tab2, b.country_id_tab2 as ctryid_tab2, c.customer_id_tab3 as custid_tab3, c.country_id_tab3 as ctryid_tab3
from table1 a
left join table2 b
on a.customer_id=b.customer_id
left join table3 c
on a.customer_id=c.customer_id
where b.customer_id is null
and c.customer_id is null
table1
customer_id country_id
101 5
102 5
103 5
104 5
105 5
106 5
107 5
108 5
109 5
110 5
table2
customer_id country_id
101 5
102 5
104 5
105 5
106 5
108 5
109 5
110 5
table3
customer_id country_id
101 5
103 5
104 5
105 5
106 5
107 5
110 5
Final output
customer_id_tab1 country_id_tab1 customer_id_tab2 country_id_tab2 customer_id_tab3 country_id_tab3
101 5 101 5 101 5
104 5 104 5 104 5
105 5 105 5 105 5
106 5 106 5 106 5
110 5 110 5 110 5
以上输出是使用上述SQL代码获得的。现在我需要用Python获得相同的输出
请帮忙
亲切问候,,
Srikanth假设您使用的是MySQL,第一步是导入必要的库,即: 现在,您可以使用一个名为:
这将为您提供结果。欢迎使用stackoverflow!请仔细阅读,并提供一个重现您的问题的示例。但是,我实际上希望使用python脚本本身连接表。这是您可以帮助的吗?请参考以下内容:
import pymysql
db = pymysql.connect(host='localhost', user='root', password='password', db='database_name', autocommit=True)
query = 'your_entire_query_to_be_inserted_here'
import pandas as pd
df = pd.read_sql(query, db)