一、条件查询
(一).字符串=某个值的查询。考察条件筛选的能力 比如:
select device_id,university
from user_profile
where university = "北京大学"
(二).其中的列大于数值 > ,可以算是运算判断,包括 <,=,in,between等
select device_id,gender,age,university
from user_profile
where age > 24
(三).考between,mysql中,是两边包含的
3.1
select device_id,gender,age from user_profile where age between 20 and 23;
3.2注意where age >=20 and age<=23不要漏写逻辑运算符AND后面的age
select device_id,gender,age
from user_profile
where age >=20 and age<=23
(四).确定集合,用来查找属性值属于指定集合的元组。
# select device_id,gender,age,university from user_profile where university <> '复旦大学'# <> 不等于 。!=是不等于的另一种写法。
# select device_id,gender,age,university from user_profile where university != '复旦大学'#用了where子句和运算符!
select device_id,gender,age,university from user_profile where university NOT IN ("复旦大学")
# where not university = '复旦大学'
# where university not like '复旦大学'
格式为:列名 [NOT] IN(常量1, 常量2,…常量n) #常量之间记得用,隔开
IN:当列中的值与IN中的某个常量值相等时,则结果为True,表明此记录为符合查询条件的记录。
NOT IN:当列中的值与某个常量值相等时,结果为False,表明此记录为不符合查询条件的记录。
例17.查询所有班名是“电子信息工程技术”、“电子声像”或“电子组装技术”的班号和班名。
SELECT 班号, 班名 FROM 班级表
WHERE 班名 IN ('电子信息工程技术' , '电子声像‘, '电子组装技术')
等价于:
SELECT 班号,班名 FROM 班级表
WHERE 班名= ‘电子信息工程技术’ OR
班名 = ‘电子声像’ OR
班名 = ‘电子组装技术’
例18.查询班名既不是“电子信息工程技术”、也不是“电子声像”和“电子组装技术”的班号和班名。
SELECT 班号,班名 FROM 班级表
WHERE 班名 NOT IN ('电子信息工程技术' , '电子声像‘, '电子组装技术')
等价于: