如何在 MySQL 中选择一行,其中多列中的某一列等于某个特定值?


为此,你可以使用多个 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)

更新于: 30-7-2019

361 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告