MySql数据表查询

来源:互联网 发布:阿里云跟阿里巴巴关系 编辑:程序博客网 时间:2024/06/03 21:04

MySQL常用操作: 增(insert)/删(delete)/改(update)查(select)

一般网站都是读多写(增删改)少。

mysql查询

分类:

  1. 单表查询 2、多表联查

    select 字段列表 from 表名 where … order by … group by … having limit ..

多表查询

  1. user表是一个常用的表(登录、注册),所以不能将一些不常用的字段(age/sex)放到user中。

多表联查分类:

  1. 利用where来进行一个多表联查

  2. 连接

    a. 内连接(inner join)    //就是等同于where    select * from user  inner join user_detail on user.id = user_detail.uidb. 左连接    会查询出左表的所有的数据,不管右表是否有与之对应的数据。    //写到left join 左边的表就是左表,会查询出坐表所有数据    select * from user left join user_detail  on user.id=user_detail.uid;c. 右连接(right join)    会查询出右表的所有数据,不管左表是否有与之对应的数据    select * from user_detail right join user on user.id = user_detail.uid
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    查询出所有用户的所有数据

其他注意问题

  1. 视图、触发器、存储过程这个在互联网项目中是不使用的。因为这三个东西效率低。

    互联网项目: 一般指的是对外的网站/app

    OA系统(上班打卡、出差申请、报销): 非互联网项目

  2. 子查询效率低,一般不使用

    //这个就是所谓子查询 
    select * from user where id in (select uid from user_detail);

重点

  1. 连接(左连接、右连接、内连接)

  2. 分组、聚合函数

    聚合函数:

    count() 统计行数max()min()avg()sum() 求总和group_concat() 得到组内的所有成员
原创粉丝点击