MySQL查询语句

1.基本查询

  • 查询指定列的数据:
    select 字段1,字段2,字段3,... from 表名 where 条件语句;

  • 定义别名:
    select 字段1 别名1,字段2 别名2,字段3 别名3,... from 表名 where 条件语句;
    select 字段1 as 别名1,字段2 as 别名2,字段3 as 别名3,... from 表名 where 条件语句;

  • 消除重复:
    select distinct 字段名 from 表明 where 条件语句;

  • 限定结果集的行数:
    select top5 字段1,字段2,字段3,... from 表名 where 条件语句; //结果集的前5行
    select top10 percent 字段1,字段2,字段3,... from 表名 where 条件语句; //结果集的前10%行

  • 计算列的值:
    select sum1,sum2,sum1+sum2 as sum from 表名 [where clause];/ /计算并显示两数和

  • 聚合函数:

函数功能
avg(<字段表达式>)求一列数据的平均值
sum(<字段表达式>)求一列数据的和
min(<字段表达式>)求列中数据的最小值
max(<字段表达式>)求列中数据的最大值
count(* I 字段名)统计查询的行数

创建图表:在这里插入图片描述
2. 条件查询

  • 用于where子句的运算符
运算符说明运算符说明
=等于<>或!=不等于
<小于<=小于等于
>大于>=大于等于
is null为空is not null不为空
and并且or或者
in包含not in不包含
between…and…两者之间not用来取非
like模糊查询(%:任意 _:一个 )----------------------------------------------------
  • 算术运算查询:
    select 字段1,字段2,字段3 frome 表名 where 条件表达式;
    例:select * from t_student where point>800;

  • 带in关键字查询:
    select 字段1,字段2 frome 表名 where 字段 [not] in(元素1,元素2);
    例:select * from score where final in (60,80);

  • 带between and的范围查询:
    select 字段1,字段2 frome 表名 where 字段 [not]between 取值1 and 取值2;
    例:select * frome score where final between 60 and 80;

  • 模糊查询:
    select 字段1,字段2… frome 表名 where 字段 [not] like ‘字符串’;
    “%”代表任意字符; “_”代表单个字符;
    例:select * frome student where sname like ‘梁%”;

  • 空值查询:
    select 字段1,字段2…frome 表名 where 字段 is[not] null;
    例:select * frome student where email is null;

  • 多条件查询(and):
    select 字段1,字段2…frome 表名 where 条件表达式1 and 条件表达式2 [and 条件表达式n]
    例:select * frome student where classno=‘080601’ and point>700;

  • 多条件查询(or)
    select 字段1,字段2…frome 表名 where 条件表达式1 or 条件表达式2 [or 条件表达式n]
    例:select * from student where classno=‘080501’ or classno=‘080601’;

  • 对查询结果排序order by:
    select 字段1,字段2…from 表名 order by 属性名 [asc|desc]
    例:select * frome student order by point desc;//降序,从大到小

  • 分组查询group by
    select 字段1, 字段2,...,聚合函数
    from 表明
    group by 字段名
    [having 条件语句];
    例:select studentno,courseno,avg(final) from score group by studentno having avg(final)>90;

  • 分页查询(limit):
    select 字段1,字段2,…from 表名 limit 初始位置,记录数;
    例:select * from student limit 0,4;

  • 合并查询(union | union all)
    select id from student
    union
    select id from score;

3.连接查询

  • select st.studentno,st.sname,st.classno,cl.department
    from student st,class cl
    where st.classno=cl.classno;

1.内连接查询(INNER)

  • select st.studentno,st.sname,st.classno,cl.department
    from student st inner join class cl
    on st.classno=cl.classno;

  • select st.studentno,st.sname,st.classno,sc.courseno,co.cname
    from student st join score sc
    on st.studentno=sc.studentno
    join course co
    on sc.courseno=co.courseno
    where st.sname='韩吟秋';

2.外连接查询OUTER(两张或以上的表连接起来查询某张表的信息)

  • 左连接查询
    select *
    from course co left join score sc
    on co.courseno=sc.courseno;

  • 右连接查询
    select *
    from course co right join score sc
    on co.courseno=sc.courseno;

4. 嵌套查询

  • 普通嵌套查询
    select * from score sc
    where sc.studentno=(select studentno from student where sname='韩吟秋');

  • 多值嵌套(ANY,ALL,IN,EXISTS)

    • 带in关键字的嵌套查询
      //查询与“何影”同班的学生学号、姓名和电话(使用子查询)
      select s.studentno,s.sname,s.phone
      from student s
      where s.classno in(
      select classno from student where sname ='何影') and s.sname!='何影';

    • 带any关键字的嵌套查询(any关键字表示满足其中任一条件)
      select * from student s where point> any(select point from student);

    • 带all关键字的嵌套查询(all关键字表示满足所有条件)
      select * from student s where point>= all(select point from student);

    • 带exists关键字的嵌套查询
      select * from student st
      where not exists(select * from score sc where sc.courseno='c05109' and st.studentno=sc.studentno);

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
MySQL是一种常用的关系型数据库管理系统,它支持使用SQL语言进行数据的查询、插入、更新和删除等操作。以下是一些常见的MySQL查询语句: 1. SELECT语句:用于从数据库中检索数据。 示例:SELECT * FROM 表名; 2. WHERE子句:用于在SELECT语句中添加条件筛选。 示例:SELECT * FROM 表名 WHERE 条件; 3. ORDER BY子句:用于对查询结果进行排序。 示例:SELECT * FROM 表名 ORDER BY 列名 ASC/DESC; 4. LIMIT子句:用于限制查询结果的数量。 示例:SELECT * FROM 表名 LIMIT 数量; 5. JOIN语句:用于在多个表之间建立关联。 示例:SELECT * FROM 表1 JOIN 表2 ON 表1.列 = 表2.列; 6. GROUP BY子句:用于对查询结果进行分组。 示例:SELECT 列名, COUNT(*) FROM 表名 GROUP BY 列名; 7. HAVING子句:用于在GROUP BY子句后添加条件筛选。 示例:SELECT 列名, COUNT(*) FROM 表名 GROUP BY 列名 HAVING 条件; 8. INSERT INTO语句:用于向数据库中插入新的数据。 示例:INSERT INTO 表名 (列1, 列2) VALUES (值1, 值2); 9. UPDATE语句:用于更新数据库中的数据。 示例:UPDATE 表名 SET 列名 = 值 WHERE 条件; 10. DELETE FROM语句:用于从数据库中删除数据。 示例:DELETE FROM 表名 WHERE 条件; 这些只是MySQL查询语句的一部分,还有很多其他的语句和功能可以用于满足不同的需求。如果你有具体的问题或者需要更详细的介绍,请告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙源lll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值