您的位置:首页 > 其它

轻松学习select查询语句(经典案例——本人自做)

2009-12-08 00:19 465 查看
-- 从当前系统中查询公用信息
select now()

-- 从表中查询所有字段
select * from pet

-- 给某些字段起别名
select `name` as aid, `sex` bid, species from pet

-- 对字段进行数学运算
select name, birth, birth+30 as manyue from pet

-- 去除掉重复记录。distinct影响到后续的所有字段
select distinct owner, name from pet

-- 多个条件用and or连接
select * from pet where owner='Gwen' or species = 'dog'

-- between and等同于>= and <=
select * from pet where birth>='1990-00-00' and birth<='1996'
select * from pet where birth between '1990-00-00' and '1996-00-00'

-- 下划线表示一个字符,%表示任意个字符
select * from pet where name not like 'b_%'

-- in表示多个条件的or
select * from pet where species='dog' or species='cat'
select * from pet where species not in ('dog', 'cat', 'bird')

-- 优先级顺序:not and or
select * from pet where (owner='gwen' or species='dog') and sex='M'

-- 列出90年代前出生的公狗和名字不以'F'开头的母猫
select * from pet where birth<'1990' and sex='m' and species='dog' or name not like 'f%' and sex='f' and species='cat'

-- 查询名字中包含'n'的人养的宠物有哪几种类型
select distinct species from pet where owner like '%n%'

-- null必须用is进行判断
select * FROM pet where death is not null

--
select * from pet where owner='gwen' order by name

-- 先按种类的降序排,再按生日的升序排
select * from pet order by species desc, birth

-- 查询条件不能用字段的别名,排序时可以使用
select name, species, birth as b
from pet
where birth>'1990'
order by b desc

-- 字符串的常见操作以字符为单位进行
select name, length(name), char_length(name),
concat(left(name,2), '...') as name
from pet

-- 单行函数可以在where条件中使用
select * from pet where left(name,1)='b'

-- 用两种方式查询出8月份出生、还没有死的宠物
-- 查询Gwen和Harold养的雌性的宠物,按主人和宠物的名字排列
-- 查询宠物的名字中包含有主人名字的第二个字母的宠物,按年龄排序
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: