简体   繁体   中英

How do I use OR and AND statements together on sqlite3?

def StatDatabase(self, x, team, position):
    # Fetches data from the FifaStats database
    # I need to add more than one variable for the different cards
    self.cur.execute("SELECT " + x + " FROM Stats WHERE Club =? AND Position =? AND Card =? Or Card =?",
                     (f'{team}', f'{position}', 'ut21  gold rare', 'ut21  gold non-rare'))
    data = self.cur.fetchall()
    if len(data) > 1:
        updated_data = random.sample(data, 1)
        for updated_info in updated_data:
            return updated_info
    else:
        for info in data:
            return info

When I execute this command, it does not recognise the fact that I want the players to be from a certain club and a certain position. In fact it retrieves most players from the database. How do I use OR statements and AND statements together?

Try using parenthesis.

SELECT *
FROM table
WHERE (column_1 = 'a' AND column_2 = 'b') OR (column_1 = 'a' AND column_2 = 'c')

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