zoukankan      html  css  js  c++  java
  • MySQL数据库之DQL(数据查询语言)

    1.MySQL之DQL查询AS CONCAT LIKE的使用

    (1)select 列名1,列名2,...... from 表名 [where 条件]

    查询所有字段用*,不带where条件的话,就会把表的所有记录查出来

    (2)过滤掉重复的列值

    select distinct 列名1 from 表名;

    (3)连接concat

    select concat(列名1,列名2) from 表名;

    select concat_ws('分隔符',列名1,列名2) from 表名;

    区别:用concat查询出来的结果不带分隔符,用concat_ws查询出来的结果带分隔符

    (4)列起别名as

    select 列名1 as 别名,列名2 from 表名;

    (5)模糊查询

    • select 列名,...... from 表名 where 列名 like '字符串';  ——精确查询
    • select 列名,...... from 表名 where 列名 like '%字符串';——左模糊查询
    • select 列名,...... from 表名 where 列名 like '字符串%';——右模糊查询
    • select 列名,...... from 表名 where 列名 like '%字符串%';——全模糊查询

    like 子句中使用百分号 %字符来表示任意字符,类似于UNIX或正则表达式中的星号 *。

    如果没有使用百分号 %, like子句与等号 = 的效果是一样的。

    2.MySQL之DQL排序以及聚合函数

    (1)排序

    select * from 表名 order by 字段名 asc;(升序,默认,可以不加)

    select * from 表名 order by 字段名 desc;(降序)

    (2)聚合函数

    select count(*) from 表名;——查询表的记录数

    select sum(列名) from 表名;——查询此列的和

    select avg(列名) from 表名;——查询此列的平均值

    select max(列名) from 表名;——查询此列的最大值

    select min(列名) from 表名;——查询此列的最小值

    3.MySQL之DQL分组group by having

    select * from 表名 group by 列名;

    select * from 表名 group by 列名 having 条件;

    4.MySQL之DQL连接查询

    (1)内连接查询

    select s.name,m.mark from student as s,mark as m where s.id=m.stu_id;

    select s.name,m.mark from student as s inner join mark as m where/on s.id=m.stu_id;

    其中,student、mark是相关联的两张表;

    (2)左连接查询

    select s.name,m.mark from student as s left join mark as m on s.id=m.stu_id;

    (3)右连接查询

    select s.name,m.mark from student as s right join mark as m on s.id=m.stu_id;

    推荐内连接

    (4)联合查询

    select name from student union all select mark from mark;

    (5)子查询

    select * from student where id in (select stu_id from mark);

    5.MySQL之DQL限制条数limit的使用

    limit 查询 限制查询的条数

    select *from 表名 limit 3;——从头数,显示三条

    select *from 表名 limit 3,5;——从头数,显示前三条的后面五条

  • 相关阅读:
    win10 下安装 tesseract + tesserocr
    win 10 家庭中文版安装docker ,但是没有 Hyper-V , 这样一步搞定
    Pycharm 分屏
    cookie 和 session
    retrying 模块
    Pychram 运行程序在 run 窗口和 python console 窗口之间切换
    封装、继承、多态
    泛型、反射、注解
    多线程笔记
    多线程
  • 原文地址:https://www.cnblogs.com/yuehouse/p/11184881.html
Copyright © 2011-2022 走看看