在 MySQL 中正确地实现 AND 条件
实现 AND 条件的语法如下 −
select *from yourTableName where yourColumnName1 = yourValue1 AND yourColumnName2 = yourValue2;
为了理解上述语法,让我们创建一个表格。创建表格的查询如下 −
mysql> create table MySQLANDConditionDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100), -> Age int -> ); Query OK, 0 rows affected (0.80 sec)
使用 insert 命令向表格中插入一些记录。查询如下 −
mysql> insert into MySQLANDConditionDemo(Name,Age) values('Larry',23); Query OK, 1 row affected (0.11 sec) mysql> insert into MySQLANDConditionDemo(Name,Age) values('Mike',21); Query OK, 1 row affected (0.17 sec) mysql> insert into MySQLANDConditionDemo(Name,Age) values('Sam',24); Query OK, 1 row affected (0.17 sec) mysql> insert into MySQLANDConditionDemo(Name,Age) values('David',26); Query OK, 1 row affected (0.17 sec) mysql> insert into MySQLANDConditionDemo(Name,Age) values('Carol',27); Query OK, 1 row affected (0.11 sec) mysql> insert into MySQLANDConditionDemo(Name,Age) values('Bob',28); Query OK, 1 row affected (0.19 sec)
使用 select 语句显示表格中的所有记录。查询如下 −
mysql> select *from MySQLANDConditionDemo;
以下是输出 −
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | Larry | 23 | | 2 | Mike | 21 | | 3 | Sam | 24 | | 4 | David | 26 | | 5 | Carol | 27 | | 6 | Bob | 28 | +----+-------+------+ 6 rows in set (0.00 sec)
以下是实现 MySQL AND 条件的查询 −
mysql> select *from MySQLANDConditionDemo where Name ='Mike' AND Age =21;
以下是输出 −
+----+------+------+ | Id | Name | Age | +----+------+------+ | 2 | Mike | 21 | +----+------+------+ 1 row in set (0.00 sec)
广告