简体   繁体   中英

For Python/MySQL, how do you perform a SELECT query on previous results?

myConnection = mysql.connector.connect(user = 'myuser', password = 'mypass', host = 'localhost', database = 'mydatabase')

# Cursor for the connection
myCursor = myConnection.cursor()   

# Assume namecolumn is a valid column...
myQuery = "SELECT * FROM mytable WHERE name = Erik"
myCursor.execute(myQuery)

# Now here, do another SELECT, but only on the results that are in myCursor

So, lets say you have a table called 'mytable'. This table also has a column called 'name'. Lets say also that there are multiple people with the name 'erik' for the column 'name'.

What I am asking is that after doing the first SELECT query, is it possible to do another SELECT query, but ONLY on the existing results of the first query? If this is possible, how? Basically, I just want to use SELECT to filter the results repeatedly.

Thanks.

将“和...”添加到原始查询或从第一个创建一个 id 列表并选择“where id in (...)”,其中 ... 是第一个查询返回的以逗号分隔的 id 列表。

也许在 WHERE 子句中尝试多个条件?

SELECT * FROM mytable WHERE name = 'Erik' AND born = 1990

Is this what you need?

myQuery = "SELECT * 
           FROM mytable
           WHERE name = 'Erik' 
           AND id in (SELECT DISTINCT id
                      FROM mytable)"

Getting distinct id or whatever other column you settle on will filter records so you avoid multiple records for the same person. It's hard to give a concrete answer given that your question is a bit vague.

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