在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 -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into MySQLANDConditionDemo(Name,Age) values('Larry',23); mysql> insert into MySQLANDConditionDemo(Name,Age) values('Mike',21); mysql> insert into MySQLANDConditionDemo(Name,Age) values('Sam',24); mysql> insert into MySQLANDConditionDemo(Name,Age) values('David',26); mysql> insert into MySQLANDConditionDemo(Name,Age) values('Carol',27); mysql> insert into MySQLANDConditionDemo(Name,Age) values('Bob',28);
使用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)
以下是对MySQLAND条件的查询-
mysql> select *from MySQLANDConditionDemo where Name ='Mike' AND Age =21;
这是输出-
+----+------+------+ | Id | Name | Age | +----+------+------+ | 2 | Mike | 21 | +----+------+------+ 1 row in set (0.00 sec)