经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MySQL » 查看文章
DQL查询数据语言(MySQL)
来源:cnblogs  作者:小孙sy  时间:2021/4/12 9:47:29  对本文有异议

 DQL查询数据语言(重点)

Select (核心)
1.查询所有字段
  select * from `表名`;
2.查询指定字段
  select `字段1`,`字段2` from `表名`;
3.给结果取一个名字
  select `字段1` as 名字1,`字段2` as 名字2 from `表名`;
4.去重distinct
  select distinct `字段` from `表名`;(例:select distinct `age` from `students`;)

where条件语句
检索符合条件的值

运算符 语法 描述
and     && a and b /  a && b

逻辑与 两个都为真结果为真

or       || a or b   /  a || b 逻辑或 一个为真结果为真
not     ! is not    /  != 逻辑非 真假反转

 select `classhour` from `subject` where `classhour` = 110 and `gradeid` =1;

select `classhour` from `subject` where `classhour` !=110; (select `classhour` from `subject` where  not `classhour`=110;)

模糊查询

 运算符  语法  描述
is null a is null  如果a为null,结果为真 
is not null  a is not null  如果a不为null,结果为真 
between  a between b and c  若a在b和c之间,结果为真 
like  a like b  SQL匹配,如果a匹配b,结果为真 
in  a in (a1,a2,a3...)  a在数列内,结果为真 

 1.like例子

  查询表中姓胡的同学    like结合(%代表任意字符,_代表一个字符)

    select `name` from `students` where `name` like '胡%';

  查询姓胡的同学,名字后面只有一个字

    select `name` from `students` where `name` like '胡_';

  查询姓胡的同学,名字后面有两个字

    select `name` from `students` where `name` like '胡__';

  查询名字中间有太字的同学  %太%

    select `name` from `students` where `name` like '%太%';

2.in例子

  查询学号为10001,10002,10003的同学

    select `name` from `students` where id in (10001,10002,10003);

3.null和not null

  查询班级号为空的人

    select `name` from `students` where `gradid` is null;

  查询学科表内学时在100和140之间的人

    select `classhour` from `subject` where `classhour` between 100 and 140;

  

    

 

原文链接:http://www.cnblogs.com/lofun/p/14598245.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号