”;
The “Logical AND” operator combines multiple conditions and evaluates to True if all the conditions are met. TinyDB Logical AND operates on two queries of a database. If both the queries are True, TinyDB will fetch the required data. On the other hand, if any one of the queries is False, it will return a blank.
Syntax
The syntax of TinyDB Logical AND is as follows −
db.search((Query().(query1) & (Query().(query2)
Here, field represents the part of data that we want to access. Query() is the object created of our JSON table named student. It will fetch the data if both the conditions met, otherwise it will return a blank.
Let”s take a couple examples and see how Logial AND works. We will use the same student database that we have used in all the previous chapters.
Example 1
Let”s see what our TinyDB Student database returns when we apply Logical AND on “st_name=lakhan” and “subject=MYSQL” field −
from tinydb import TinyDB, Query db = TinyDB(''student.json'') db.search ((Query().st_name == ''lakhan'') & (Query().subject == ''MySQL''))
This query will fetch only those rows where the student name is “lakhan” and the “subject” is “MySQL”.
[{ ''roll_number'': 4, ''st_name'': ''lakhan'', ''mark'': 200, ''subject'': ''MySQL'', ''address'': ''mumbai'' }]
Example 2
In this example, let”s apply Logical AND on the “subject” and “roll_number” fields −
from tinydb import TinyDB, Query student = Query() db = TinyDB(''student.json'') db.search((student.subject.search(''M'')) & (student.roll_number < 5))
This query will fetch all the rows where the roll_number is less than “4” and “subject” starts with the letter “M”.
[{ ''roll_number'': 4, ''st_name'': ''lakhan'', ''mark'': 200, ''subject'': ''MySQL'', ''address'': ''mumbai'' }]
”;