MySQL中是否可以像“SELECT * FROM table WHERE condition1 and condition2”的形式使用两个where子句?
是的,您需要使用AND或OR运算符。语法如下:
select *from yourTableName where yourColumnName1=yourValue AND
yourColumnName=yourValue';
对于AND条件,两个条件都必须成立,否则您将得到一个空集。
要理解上述语法,请让我们创建一个表。创建表的查询如下所示−
mysql> create table WhereDemo
-> (
-> Id int,
-> Name varchar(20)
-> );
Query OK, 0 rows affected (0.56 sec)
现在,您可以使用insert命令在表中插入一些记录。查询如下所示−
mysql> insert into WhereDemo values(101,'Maxwell');
Query OK, 1 row affected (0.14 sec)
mysql> insert into WhereDemo values(110,'David');
Query OK, 1 row affected (0.21 sec)
mysql> insert into WhereDemo values(1000,'Carol');
Query OK, 1 row affected (0.18 sec)
mysql> insert into WhereDemo values(1100,'Bob');
Query OK, 1 row affected (0.47 sec)
mysql> insert into WhereDemo values(115,'Sam');
Query OK, 1 row affected (0.23 sec)
使用select语句显示表中的所有记录。查询如下所示−
mysql> select *from WhereDemo;
以下是输出−
+------+---------+
| Id | Name |
+------+---------+
| 101 | Maxwell |
| 110 | David |
| 1000 | Carol |
| 1100 | Bob |
| 115 | Sam |
+------+---------+
5 rows in set (0.00 sec)
下面是选择具有多个条件的表中的所有记录的查询−
mysql> select *from WhereDemo where Id=1100 AND Name='Bob';
以下是输出−
+------+------+
| Id | Name |
+------+------+
| 1100 | Bob |
+------+------+
1 row in set (0.00 sec)
阅读更多:MySQL 教程
极客教程