Started Python: Swap an array of values obtained from SQL results to a list type and use it in IN of another query

First, replace the value array of a certain SQL result with the list type.

Sample query

users_id_query = '''
SELECT CAST(user_id as char) as user_id FROM users_table;
'''

Execute the above query in Python.

users_ids = execute_query('my_database', users_id_query)

Why do you want the result to look like "111,222,333":

users_result = [str(x['user_id']) for x in users_ids['rows']]
users_ids_string = ','.join(users_result)

The users_ids_string is nicely filled with comma-separated values that you want.

Use the prepared list in IN of another query

Sample SQL

users_login_query = '''
SELECT count(0) FROM login_log_table where user_id in (user_ids_string)
'''

"User_ids_string" is the parameter to replace with the actual value next.

entry_users_ids = execute_query('my_database', users_login_query.replace('user_ids_string', entry_users_ids_string))    

Replace the value at the same time while specifying the execution destination.

Recommended Posts

Started Python: Swap an array of values obtained from SQL results to a list type and use it in IN of another query
How to swap elements in an array in Python, and how to reverse an array.
Python: Create a dictionary from a list of keys and values
Various ways to create an array of numbers from 1 to 10 in Python.
How to use Python Kivy ③-Linkage with WebAPI (from sending and receiving requests to displaying a list of results, transitioning multiple screens)-
Connect to postgreSQL from Python and use stored procedures in a loop.
Extract every n elements from an array (list) in Python and Ruby
[Python] Concatenate a List containing numbers and write it to an output file.
How to save the feature point information of an image in a file and use it for matching
Get the value of a specific key in a list from the dictionary type in the list with Python
I want to use a python data source in Re: Dash to get query results
How to write a list / dictionary type of Python3
How to check in Python if one of the elements of a list is in another list
[Python] How to delete rows and columns in a table (list of drop method options)
Recursively get the Excel list in a specific folder with python and write it to Excel.
An easy way to view the time taken in Python and a smarter way to improve it
How to slice a block multiple array from a multiple array in Python
Temporarily save a Python object and reuse it in another Python
Comparison of how to use higher-order functions in Python 2 and 3
How to get a list of built-in exceptions in python
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
[Machine learning] Understand from mathematics that standardization results in an average of 0 and a standard deviation of 1.
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
Create an instance of a predefined class from a string in Python
processing to use notMNIST data in Python (and tried to classify it)
Try to get a list of breaking news threads in Python.
How to make a string into an array or an array into a string in Python
[Python] How to swap array values
[Python / Jupyter] Translate the comment of the program copied to the clipboard and insert it in a new cell
The story of making a tool to load an image with Python ⇒ save it as another name
I want to pass an argument to a python function and execute it from PHP on a web server
Tips: [Python] Randomly restore and extract an array from a fasta file
[Python] How to put any number of standard inputs in a list
I want to color a part of an Excel string in Python
Receive a list of the results of parallel processing in Python with starmap
How to format a list of dictionaries (or instances) well in Python
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
An engineer who has noticed the emo of cryptography is trying to implement it in Python and defeat it
Display a list of alphabets in Python 3
Summary of how to use Python list
How to use is and == in Python
How to pass the execution result of a shell command in a list in Python
[Python] Create a list of date and time (datetime type) for a certain period
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
A complete guidebook to using pyenv, pip and python in an offline environment
[Python] How to easily drop a child process started by multiprocess from another process
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
How to get a list of files in the same directory with python
A memo to generate a dynamic variable of class from dictionary data (dict) that has only standard type data in Python3
How to clear tuples in a list (Python)
Make a copy of the list in Python
Summary of how to use MNIST in Python
Output in the form of a python array
Randomly select elements from list (array) in python
List of Python code to move and remember
[Python] Manipulating elements in a list (array) [Sort]
A Python script that crawls RSS in Azure Status and posts it to Hipchat
What is a dog? Django--Volume of using values obtained from URLs in class-based views
Differences in behavior between append () and "+ =" operators when adding data to a list in Python
How to identify the element with the smallest number of characters in a Python list?
Created a Python library to write complex comprehensions and reduce in an easy-to-read manner