⊗pyPmDBSVS 112 of 128 menu

Selecting Records by Specific Values ​​in Python

If you want to select a record from a group of field values, you can use the IN command. For example, you can select records where the name field has the value 'user1', 'user3', or 'user4'. This would look like this: WHERE id IN ('user1', 'user3', 'user4').

In principle, for such things you can also use the command OR, then the first example will look like this: WHERE id=1 OR id=3 OR id=7 OR id=14 OR id=28. The option with IN still looks simpler and more elegant.

Example

Let's select users whose field id has the values ​​1, 3, 5:

query = "SELECT * FROM users WHERE id IN (1, 3, 5)"

The result of the executed code:

{'id': 1, 'name': 'user1', 'age': 23, 'salary': 400} {'id': 3, 'name': 'user3', 'age': 23, 'salary': 500} {'id': 5, 'name': 'user5', 'age': 27, 'salary': 500}

Example

The IN command can be combined with other commands in a query. Let's rewrite the previous example, specifying that the users' age must be greater than 23 years:

query = "SELECT * FROM users WHERE id IN (1, 3, 5) AND age>23"

The result of the executed code:

{'id': 5, 'name': 'user5', 'age': 27, 'salary': 500}

Practical tasks

Get users whose salary has values ​​of 400 and 900.

Get users whose age is 25, 27 and 28 years old. At the same time, the id is more than 2.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline