user10508414
user10508414

Reputation:

How to give output from one query to next query

How to give output from one query to the next query

My out database mysql_db, employee table I will get the id's select id from the employee

df['out']

         id
0        20017
1        20046
2        20047
...        ...
24337    47883
24338    47884

get the id from table one and apply on the mysql_db2 employee table where its present or not

select * from employee where id in ('df['out']')

Upvotes: 0

Views: 35

Answers (1)

KenHBS
KenHBS

Reputation: 7174

If you cannot join the two tables, because they are in different databases, I would manipulate a string with the values of df['out'] like this:

valid_employee_ids = tuple(df['out'])
cmd = f"""
   SELECT
     *
   FROM
     employee
   WHERE
     id IN {valid_employee_ids}
"""

Which gives you the following string:

SELECT
  *
FROM
  employee
WHERE
  id IN (20046, 20047, 47883, 47884)

Note on f-strings: The string manipulation above uses f-strings, which are only available as of Python 3.5. Alternatively, you could also use .format() like this:

cmd = "SELECT * FROM employee WHERE id IN {}".format(valid_employee_ids)

Upvotes: 1

Related Questions