找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
一、怎么连接Navicat
1.查看MySQL有没有远程连接用户——%
    a.进入MySQL主界面,进入MySQL数据库:use mysql;
    b.查询MySQL数据库创建了那些用户:select host,user from user;
    c.查看用户列表里有没有 % 用户
    d.若有则跳过步骤2,若没有则执行步骤2
2.在MySQL创建
    a.创建用户授权: grant all privileges on *.* to 'root'@'%' identified by '123456';
    b.解释:创建的用户名:root 密码:123456 权限:% 可远程连接
3.重启数据库
    a.在liunx界面重启数据库:service MySQLd restart;
4.关闭防火墙
    a.在liunx界面关闭防火墙:service iptables stop;
5..在Navicat连接数据库
    a.输入要连接的数据库ip地址,测试连接,通过就OK了
    b.例如:连接名                随意                数据库
                域名或ip        ifconfig查到的地址
                端口                默认的就行        3306
               用户名        root
               密码                123456



1、创建用户账号的同时赋予权限:
    用户名:root        密码:123456                权限:远程访问权限%
    grant all privileges on *.* to 'root'@'%' identified by '123456';
2、只创建用户,不赋予权限:
    新增一个用户,赋予本地访问权限,用户名:qian
    insert into user (host,user,password) values('localhost','qian',password(123456));
3、给用户账号赋予权限:
    geant select,update,delete,drop on *.* to 'qian '@'localhost' identified by '123456';
4、查询指定用户拥有哪些权限
    show grants for 'qian'@'localhost';
5、取消用户权限
    revoke all on *.* from 'qian'@'localhost';
6、删除用户
    delete from user where user='qian' and host='localhost';
7、修改用户密码
    在user表中把root用户的密码改123456
    update user set password=password('123456') where user='root';


二、多表查询的方法(重点):

    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面试题经常问的一个问题:左右连接的区别?
相同点:
1.一方为主表,另一方则为子表
2.如果主表的值是大于子表的值,子表缺少的值自动补齐为null
3.如果主表的值是小于子表的值,只取和主表相同的值其它不显示
不同点:
1.左连接左表是主表,右链接右表是主表。
1.左连接显示左表的所有数据和右表中满足where条件的数据。
2.右连接显示右表的所有数据和左表中满足where条件的数据。



union连接方法                :把选中所有的表内容并在一起显示,选中的表必须结构类型相同。
select * from aa union select * from cc;




举一反三:左右连接内连接
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,dcore fom (select*from aa left join cc on id=s_id) where name='zhangsan';
求出谁没参加考试:select name,score from (select * from aa left join cc on id=s_id)q  where score is null;

分享至 : QQ空间
收藏

0 个回复

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