找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
一、连接需满足的条件
账号:   root
密码: 123456
端口:3306
域名、ip地址:ifconfig查询ip
数据库重启:service mysqld restart
防火墙需要关闭:service iptables stop
且对应的账号要拥有远程访问的权限(%)百分号权限
如果没有(%)权限需要通过指令来赋予;
1.use mysql   :先进入数据库
2.select host,use from user:在查询即可
如果没有远程权限:grantall privileges on  *.* to 'root'@'%' identified by '123456';
3.创建用户不赋予权限
新增加一个用户赋予本地访问权限,用户名称是qian。
insert into user (host,user,password)values('localhost','qian',password('123456'));
4。创建用户后进行授权(方法二)
grant select,update,delete,drop on *.* to 'qian'@'localhost' identified by '123456'
5、查询指定用户拥有哪些权限
show grants for 'qian'@'localhost';
6.取消指定用户的权限
revoke all on *.* from 'qian'@'localhost';
7.删除用户
delete from user where user='qian' and host='localhost';
8.改用户密码
在user表中把root用户密码改为123456
update user set password=password('123456') where user='root';

二、多表查询的方法(重点)
A          姓名表                       C                   成绩表
id           name                         s_id        scroe
1        a                                   1                 100
2        c                                   2                    20
3        d                                 1               30
查找张三的成绩
select * from a;
1、多表的条件:必须要关联的表中有相同的字段
2、有哪些方法可以把表和表进行关联?
基本连接:常用的方法
select *from aa,cc where aa.id=cc.s_id;    #表关联
select *from aa ,cc where id=s_id;     #表关联
select *from cc,aa where id=s_id;     #from后面表是可以随意的排序
注意:查询出来的结果只会显示两个表中有的值;
两两为真——为真
一真一假——为假
一假一真——为假
两两为假——为假
内连接:inner join on
select * from aa inner join cc on aa.id=cc.s_id;
select * from aa inner join cc on id=s_id;
select * from cc inner join aa on id=s_id;
查询出来的结果只会显示两个表中有的值,
两两为真为真  
一真一假,一假一真,两两为假为假
左连接方法:左边的表为主表,右边的表为子表
left join on
select *from aa left join cc on aa.id=cc.s_id;   主表;aa   子表;cc
select * from aa left join cc on id=s_id;主表:aa 子表cc
select * from cc left join aa on id=s_id;主表:cc 子表aa
from后面是左边   on的前面是右边
解释:
左连接以左边表为基准
如果主表的值是大于子表的值,字表缺省的值自动补齐null
如果主表的值是小于子表的值,只取和主表相同的值其它不显示
右连接:右边的表为主表,左边的表为子表;
right join on
select *from aa right join cc on aa.id=cc.s_id;  主表:cc    子表;aa
select * from aa right join cc on id=s_id;主表:cc 子表aa
select * from cc right join aa on id=s_id;主表:aa 子表cc
解释:
右连接是以右表为主表
如果主表的值是大于子表的值,子表缺省的值自动补齐null
如果主表的值是小于子表的值,只取和主表相同的值其它不显示
MySQL面试题经常问的一个问题:左右连接的区别?
左连接是以左表为参照,把左表的内容全部显示出来,没有的数据以null显示
右连接是以右边表为参照,把右表的内容全部显示出来,没有的数据以null显示
union连接方法
select * from aa union select * from cc;
此方法连接是必须多个表字段要相同

例题
求出张三的成绩:
+------+----------+
| id   | name     |  姓名表 aa
+------+----------+
| 1001 | zhangsan |
| 1002 | lisi     |
+------+----------+

+------+-------+   成绩表cc
| s_id | score |
+------+-------+
| 1001 | 99    |
+------+-------+

举一反三:左右连接内连接
select * from aa,cc where id=s_id; #两个表进行拼接
| 1001 | zhangsan || 1001 | 99    |
select * from aa,cc where id=s_id and name='zhangsan';
| 1001 | zhangsan || 1001 | 99    |
select score from aa,cc where id=s_id and name='zhangsan';
| 99    |

临时表写法:
select * from aa,cc where id=s_id;
| 1001 | zhangsan || 1001 | 99    |
select * from (select * from aa,cc where id=s_id)a;
| 1001 | zhangsan || 1001 | 99    |
select * from (select * from aa,cc where id=s_id)a where a.name='zhangsan';
select score from (select * from aa,cc where id=s_id)a where a.name='zhangsan';

select * from (select * from aa,cc where id=s_id)a where name='zhangsan';

举例:
select * from aa order by score desc group by class;如果出现抛异常语法不能这样
select * from(select * from aa order by score desc)a group by class;参考一下临时表的方法

select max(score),name,clss from aa;
select name,class from(select max(score),name,clss from aa)a;

嵌套:
select * from aa where name='zhangsan';张三的所有信息
| 1001 | zhangsan |
select id from aa where name='zhangsan';
| 1001 |
select * from cc;
| 1001 | 99    |
select * from cc where s_id in(select id from aa where name='zhangsan');
| 1001 | 99    |
select score from cc where s_id in(select id from aa where name='zhangsan');
| 99    |

求出谁没参加考试?
select name,score from (select * from aa left join cc on id=s_id)a where score is null;
select name,score from (select * from cc right join aa on id=s_id)a order by score asc milit 1;

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册