- TinyDB - Discussion
- TinyDB - Useful Resources
- TinyDB - Quick Guide
- TinyDB - Extensions
- TinyDB - Extend TinyDB
- TinyDB - Middleware
- TinyDB - Storage Types
- TinyDB - Caching Query
- TinyDB - Default Table
- TinyDB - Tables
- TinyDB - Document ID
- TinyDB - Retrieving Data
- TinyDB - Upserting Data
- TinyDB - Modifying the Data
- TinyDB - Handling Data Query
- TinyDB - Logical OR
- TinyDB - Logical AND
- TinyDB - Logical Negate
- TinyDB - The one_of() Query
- TinyDB - The All() Query
- TinyDB - The Any() Query
- TinyDB - The Test() Query
- TinyDB - The Matches() Query
- TinyDB - The Exists() Query
- TinyDB - The where Clause
- TinyDB - Searching
- TinyDB - Querying
- TinyDB - Delete Data
- TinyDB - Update Data
- TinyDB - Retrieve Data
- TinyDB - Insert Data
- TinyDB - Environmental Setup
- TinyDB - Introduction
- TinyDB - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
TinyDB - Logical AND
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 }]Advertisements