MySQL单表查询多表查询

单表查询和多表查询

一:单表查询

(1)查询字段,可以指定select *或者某些字段名
(2)查询记录,通过where限定一个条件。如取出dept_no=20的e_name和e_job信息
obj.selectSql('select e_name,e_job from employee where dept_no=20')
(3)and or语句,and表示要满足多个条件同时成立,or表示满足其中任意一个成立,使用这两种语法可以把每个条件用小括号括起来,避免逻辑混乱
(4)in查询,对where条件的扩展。指定条件等于in中的某一项,如选出depy=20或者30的所有信息
obj.selectSql('select * from employee where dept_no in (20,30)')
如果不适用in 那么需要指定dept_no=20 or dept_no=30  太麻烦,not in同理
(5)between and查询,也是对where的扩展。如选出e_no在1003到1007中间的信息
obj.selectSql('select * from employee where e_no between 1003 and 1007')
也可用e_no>=1003 and e_no<=1007实现,一样过于麻烦
(6)查询空值is null,同样是对where条件的扩展,如果是空值,则条件成立输出该条记录,is not null同理
(7)like语法:_和%都是万能字符,_只能代表一个字符,%可以代表任意多个。如选出e_name中有s开头的记录
obj.selectSql('select * from employee where e_name like "s%"')
(8)distinct去重语句,这个语句一般只用于单个字段查询才有用(第七章专家点评第一个详解)。
(9)order by语句:order by可以完成单列排序,多列排序(排序顺序,先来后到),指定方向的排序。如查看employee表中所有信息,先对e_job降序排列,再对e_salary升序排列
obj.selectSql('select * from employee order by e_job desc,e_salary')
先对e_job降序排序然后对e_salary升序排列,可以试试先排e_salary后排e_job的结果
(10)Group by语句:可以完成单字段分组,多字段分组(注意分组顺序),having过滤,with rollup
单字段分组,查出所有的职位e_job及职位数量
obj.selectSql('select e_job,count(*) from employee group by e_job')
多字段分组,查出所有的职位e_job中不同工作部门dept_no的数量
obj.selectSql('select e_job,dept_no,count(*) from employee group by e_job,dept_no')
having用法,查出所有的职位e_job中不同工作部门dept_no的数量大于1的记录
obj.selectSql('select e_job,dept_no,count(*) from employee group by e_job,dept_no having count(*)>1')
having和where都是过滤语句,区别在于:having在数据分组之后过滤,而where是在数据分组之前过滤,且where过滤掉的数据不会再参与分组。所以where语句应放在Group语句前
obj.selectSql('select e_job,dept_no,count(*) from employee where dept_no=20 group by e_job')
with rollup就是在所有查询出来的分组记录后再添加一个记录,统计记录数量
obj.selectSql('select e_job,dept_no,count(*) from employee where dept_no=20 group by e_job with rollup')
order by和group by的结合使用,先分组,后对每个分组进行排序,按顺序写就好
(11)limit语句 限制查询数量。如从所有信息的第3条开始查询,查询4条记录,第一个参数不写默认为0
obj.selectSql('select * from employee limit 2,4')
(12)union语句,将查询相同字段不同条件的结果合并在一起并且去除掉重复的查询结果,union all不去重但效率比union高
obj.selectSql('select * from employee where dept_no=20 union select * from employee where e_salary>2000')
union可以使用or语句代替
obj.selectSql('select * from employee where dept_no=20 or e_salary>2000')
二:多表查询  使用dept和employee表
(i):连接查询
 (1)内连接:如查询e_gender为f的员工的所有信息(包括dept和employee的所有信息)
内连接就是对两个表的交集做一些条件限制
语法1:
obj.selectSql('select * from dept,employee where dept.d_no=employee.dept_no and employee.e_gender="f"')
语法2:
obj.selectSql('select * from dept inner join employee on dept.d_no=employee.dept_no and employee.e_gender="f"')
语法2是sql标准语法,一般使用这种语法
 (2)左右连接
左连接:左表说我最厉害,必须保持我的完整,右表你过滤过之后要向我看齐,我有你没有的你赶快和我看齐补空值,你有我没有的赶快扔了
如以左连接查询dept和employee中e_gender为f的员工信息
obj.selectSql('select * from dept left join employee on dept.d_no=employee.dept_no and employee.e_gender="f"')
右连接:如以右连接查询dept和employee中e_gender为f的员工信息
obj.selectSql('select * from dept right join employee on dept.d_no=employee.dept_no and employee.e_gender="f"')
(ii)子查询 (能用连接查询代替尽量不使用子查询,效率低)
 (1)带有any,some的子查询,关键字接在一个比较操作符后,表示若与子查询返回的其中一个值比较为true则返回true
 (2)带有all的子查询,关键字接在一个比较操作符后,表示若与子查询返回的所有值比较均为true才返回true
 (3)exists关键字后跟一个子查询,如果查询到东西则返回true,外查询开始执行
 (4)in关键字子查询返回一个序列,功能与单表查询中的in语句相同
三:其他
  (1)函数:常用的有count(),max(),avg(),min(),sum()
  (2)as语法:as语法给字段或者表改名,语法:字段名或列名 as 新字段名或列名
  (3)正则语法查找:使用regexp关键字,语法和python正则语法相同,可以和like做下对比
obj.selectSql('select * from employee where e_name regexp "^s"')
obj.selectSql('select * from employee where e_name like "s%"')
  (4)group by之后如果想查看某个分组内的某个字段内容可以用group_concat
如  查看所有职位的工作人员名字
obj.selectSql('select e_job,count(*) as total,group_concat(e_name) as name from employee group by e_job')
如果不使用group_concat 直接查看e_name 返回的是该分组内其中一个人的名字


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值