简体   繁体   中英

SQL Query Adding Complex Conditions from a Table

I have a long list (or a table of 3 to 4 columns of data) which simultaneously needs to be satisfied in a query, like below:

A    B   C
x1   y1  z1
x2   y2   z2
x3   y3   z3
.
.
.
xn    yn   zn

Is there a way I can create an expression/function etc or separate list etc which I can have which can be called into the SQL Query (instead of explicitly writing it):

So assuming, I have this list as a matrix/list in a table X (local) and I can use

and I can use a sample SQL query something like

SELECT *
FROM TABLE B
WHERE CONDITION  IN  ROWS OF TABLE X 

Just to add here, that I am using python driver to get data via database and the table X (I have it locally on my drive) which I have is which I created locally.

I am assuming you are asking for python code.

Using a simple in-memory SQLite db you can achieve this. See how use Sqlite db with python here. How to work with sqlite3 and Python .

Considering your example where we have list of data.

import sqlite3
connection = sqlite3.connect("company.db")

cursor = connection.cursor()

staff_data = [ ("William", "Shakespeare", "m", "1961-10-25"),
               ("Frank", "Schiller", "m", "1955-08-17"),
               ("Jane", "Wall", "f", "1989-03-14") ]

for p in staff_data:
    format_str = """INSERT INTO employee (staff_number, fname, lname, gender, birth_date)
    VALUES (NULL, "{first}", "{last}", "{gender}", "{birthdate}");"""

    sql_command = format_str.format(first=p[0], last=p[1], gender=p[2], birthdate = p[3])
    cursor.execute(sql_command)

See more here .

.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM