如何在 MySQL 中选取多列中有一列等于某个值的行?

mysqlmysqli database

为此,您可以使用多个 OR。让我们首先创建一个表 −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(10),
   LastName varchar(10),
   Age int,
   CountryName varchar(10)
   );
Query OK, 0 rows affected (0.58 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable(FirstName,LastName,Age,CountryName)
values('John','Smith',21,'US');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(FirstName,LastName,Age,CountryName)
values('Carol','Taylor',22,'AUS');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable(FirstName,LastName,Age,CountryName)
values('David','Miller',19,'UK');
Query OK, 1 row affected (0.16 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

输出

+----+-----------+----------+------+-------------+
| Id | FirstName | LastName | Age  | CountryName |
+----+-----------+----------+------+-------------+
| 1  | John      | Smith    | 21   | US          |
| 2  | Carol     | Taylor   | 22   | AUS         |
| 3  | David     | Miller   | 19   | UK          |
+----+-----------+----------+------+-------------+
3 rows in set (0.00 sec)

以下查询用于选择其中某一列等于某个值的行 −

mysql> select *from DemoTable where FirstName="Carol" OR Age=22 OR CountryName="AUS";

输出

+----+-----------+----------+------+-------------+
| Id | FirstName | LastName | Age  | CountryName |
+----+-----------+----------+------+-------------+
| 2  | Carol     | Taylor   | 22   | AUS         |
+----+-----------+----------+------+-------------+
1 row in set (0.00 sec)

相关文章