Tinydb 简明教程

TinyDB - Logical OR

“逻辑 OR”运算符结合多个条件,且仅在满足任一条件时才计算为 True。TinyDB 逻辑 OR 对数据库的两个查询进行操作。如果其中任何一个查询为真,则 TinyDB 将获取所需的数据。另一方面,如果两个查询都为假,则它将返回一个空值。

The "Logical OR" operator combines multiple conditions and evaluates to True only if either of the condition is met. TinyDB Logical OR operates on two queries of a database. If any one of the queries is True, TinyDB will fetch the required data. On the other hand, if both the queries are False, it will return a blank.

Syntax

TinyDB Logical OR 的语法如下所示 −

The syntax of TinyDB Logical OR is as follows −

db.search((Query().(query1) | (Query().(query2)

其中 field 表示我们要访问的数据部分。 Query() 是我们名为 student 的 JSON 表创建的对象。它将在满足任何一个条件时获取数据,否则将返回一个空值。

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 any one of the conditions is met, otherwise it will return a blank.

让我们举几个例子,看看它是如何工作的。我们将使用我们在所有先前章节中使用的 student 数据库。

Let’s take a couple of examples and see how it works. We will use the same student database that we have used in all the previous chapters.

Example 1

让我们看看当我们在“st_name”和“subject”字段上应用逻辑 OR 并检查以下条件时,我们的 TinyDB 学生数据库返回什么:st_name=lakhan 和 subject=TinyDB −

Let’s see what our TinyDB Student database returns when we apply Logical OR on the "st_name" and "subject" fields and check the following conditions: "st_name=lakhan" and "subject=TinyDB" −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
db.search ((Query().st_name == 'lakan') | (Query().subject == 'TinyDB'))

此查询将获取以下行 −

This query will fetch the following rows −

[
   {
      "roll_number":1,
      "st_name":"elen",
      "mark":250,
      "subject":"TinyDB",
      "address":"delhi"
   },
   {
      "roll_number":4,
      "st_name":"lakhan",
      "mark":200,
      "subject":"MySQL",
      "address":"mumbai"
   },
   {
      "roll_number":5,
      "st_name":"karan",
      "mark":275,
      "subject":"TinyDB",
      "address":"benglore"
   }
]

Example 2

此示例中,让我们对“subject”和“roll_number”字段应用逻辑 AND −

In this example, let’s apply Logical OR on the "subject" and "roll_number" fields −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
db.search((student.subject.search('M')) | (student.roll_number < 5))

此查询将获取其中“subject”字段以字母“M”开头或“roll_number”小于“5”的所有行。

This query will fetch all the rows where the "subject" field starts with the letter "M" or the "roll_number" is less than "5".

[
   {
      "roll_number":1,
      "st_name":"elen",
      "mark":250,
      "subject":"TinyDB",
      "address":"delhi"
   },
   {
      "roll_number":2,
      "st_name":"Ram",
      "mark":[
         250,
         280
      ],
      "subject":[
         "TinyDB",
         "MySQL"
      ],
      "address":"delhi"
   },
   {
      "roll_number":3,
      "st_name":"kevin",
      "mark":[
         180,
         200
      ],
      "subject":[
         "oracle",
         "sql"
      ],
      "address":"keral"
   },
   {
      "roll_number":4,
      "st_name":"lakhan",
      "mark":200,
      "subject":"MySQL",
      "address":"mumbai"
   }
]