简体   繁体   中英

How do I create a loop, or a way to use the results of previous SELECT statements into the next SELECT statement?

I have a database with a nested hierarchy structure. At the high levels of the hierarchy, there aren't any actual typeIDs (individual items) associated with a given marketGroupID .

Instead, I start at the top using a string search to find the categories I want. I need to keep going down in the hierarchy by using the marketGroupIDs I've selected as the parentGroupIDs for the next search so that I find all IDs within that subcategory. This keeps dividing into more subcategories until the column hasTypes = 1 (there are typeIDs associated with this subcategory).

While I'm currently pursuing a very manual option: keep making another search until I get to the level I want, is there a way to construct a loop that keeps SELECTING based upon WHERE marketGroupID X = parentGroupID Y, and hasTypes = 0 ?

MANUAL METHOD I'M TRYING TO REPLACE

The columns I am mainly working with are marketGroupID and parentID. What I've tried so far is using c.fetchall() to get the previous result and put it in a variable next_input. Then I use the parameter '?' using sqlite3 in Python. The issue with this is that the number of inputs can change, as the previous SELECT statement will give me a list of tuples that could be anywhere from sixty to several hundred long.

c.execute("""SELECT marketGroupID FROM invMarketGroups WHERE parentGroupID IN (SELECT marketGroupID FROM invMarketGroups WHERE marketGroupName='Ships' 
OR marketGroupName = 'Ship Equipment' OR marketGroupName = 'Implants & Boosters')""");

Next search step.

python next_input = c.fetchall()

So, taking the time to make the number of these equal to list length, I've managed to make it work by turning the result into a single list:

next_input = [i[0] for i in next_input]

c.execute("SELECT marketGroupID FROM invMarketGroups WHERE parentGroupID IN ({idlist_formatted})".format(idlist_formatted= ','.join(['?']*len(next_input))), next_input)

How do I make a loop so that it keeps running recursively that ends until hasTypes != 0, grabbing marketGroupIDs where hasTypes = 1?

Sqllite supports recursive queries which can be used for hierarchical queries (which is what you are trying to do). Take a look at the documentation here It even has a section about hierarchical queries. This way you can completely replace your loop with one call to the DB and let it do the work.

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