Skip to content
Advertisement

Tables being queried with SQL and Sqlalchemy have same column names

I am querying a variety of different tables in a mysql database with sqlalchemy, and sql query code.

My issue right now is renaming some of the columns being joined. The queries are all coming into one dataframe.

All i actually want to get from those tables is a single column added to my query. Each table has a column with the same name. My approach to using an alias has been as below,

But the variety of ways I’ve tried to implement this ends up with annoying errors.

I am querying around 20 different tables as well, so using SELECT * at the beginning while inneficient is ideal for the sake of ease.

The output I’m looking for is a dataframe that has each of the columns I need in it (which I am trying to then filter and build modeling with python for). I am fine with managing the query through sqlalchemy into pandas, the alias is what is giving me grief right now.

Thanks in advance,

Advertisement

Answer

You can use nested queries:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement