'Query to call values matching a column in a local dataframe with mysql
so I am using PyMySQL to access an SQL database where I want to extract data from a dataframe matching the id's of a column I have in a local dataframe
local_df
id
1
3
11
.
sql_df
id feat
1 f
2 g
3 d
4 g
5 q
6 p
. .
is it possible to create a query that extracts only the rows with matching id's?
something like
query = """
SELECT *
FROM sql_df
WHERE id(local_df) = id(sql_df)
"""
sorry for the messy representation
Solution 1:[1]
You just need an INNER JOIN
SELECT sql_df.*
FROM sql_df
INNER JOIN local_df on sql_df.id =local_df.id
You have other ways using EXIST or IN like:
SELECT sql_df.*
FROM sql_df
WHERE EXISTS (select null from local_df where sql_df.id=local_df.id)
Using IN
SELECT sql_df.*
FROM sql_df
WHERE id in (select distinct id from local_df)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ergest Basha |
