欢迎来到贱贱的博客

扩大
缩小

Mysql笔记5之查询

1查询所有的列

  select *from student

2查询指定列

  select name,age from student

3查询时候使用别名

  select name as 别名,age as 年龄 from student  as可以省略

4查询增加常量列

  //查询的时候加上一列专业

  select id,name,age,addr,'就业办' as 专业 from student

5查询合并列

  select name,(math+english) as 总成绩 from student

  select name+addr from student //此时不能合并

6查询去重

  select distinct age from student'

  select distinct(age) from student

7条件查询

  (1)and or

    select *from student where name="李四" and age = 30

  (2)查询范围

    select *

       from student

          where 1=1

              and math>70

  (3)between

    select *from student where math between 78 and 98

  (4)查询为空的记录   、

    SELECT * FROM student;

    -- 增加备注列

    ALTER TABLE student ADD COLUMN remarks VARCHAR(50);

    -- sid1的学生添加一个“新增加”备注

     UPDATE student SET remarks='新增加' WHERE sid=1;

    -- sid2的学生设置空字符串

    UPDATE student SET remarks='' WHERE sid=2;

    -- 需求: 查询备注不为空的学生(包括空字符串和null)

     SELECT * FROM student WHERE remarks IS NULL OR remarks='';

     (5)模糊查询

        a:使用like关键字

        B:%代表任意字符

        c:_代表一个字符

          -- 需求: 查询姓'',且名字只有两个字的学生

          SELECT * FROM student WHERE sname LIKE '_';

          -- 注意:null:数据没有修改过    空字符串: 被修改过

          SELECT * FROM student WHERE remarks IS NOT NULL  AND remarks!='';

8查询排序(默认为增序)

  select *from student order by 列名 asc//升序

  select *from student order by age desc//降序

  多个条件排序

    按照年龄升序,按照servlet成绩降序

    SELECT * FROM student ORDER BY age ASC,math DESC;

9注意注意  查询返回限定行

  (1)查询返回限定行

    select *from student limit 4;//返回行数

      //查询第3 4行的数据

        注意:第一个参数:查询的起始行(从0开始算的)

           第二个参数:查询的行行素

      //查询第一二行的数据

        select *From student limit 0,2

10 使用聚合函数查询

  (1)查询math成绩最高

    select max(math) from student

  (2)查询jsp平均成绩

      select avg(jsp) from student;

11 分组查询

  //查询人数大于2的小区重庆万州

    select address,count(sid) 人数 from student GROUP BY address HAVING COUNT(sid) >2

 

 

    

posted on   L的存在  阅读(207)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示