Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to call values matching a column in a local dataframe with mysql

Tags:

python

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

like image 656
andriy Avatar asked Feb 04 '26 11:02

andriy


1 Answers

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)
like image 90
Ergest Basha Avatar answered Feb 06 '26 05:02

Ergest Basha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!