SQL中如何使用DQL查询语言,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

DQL

DQL:data Query language 数据查询语言

格式:select[distinct] 字段1,字段2 from 表名 where 控制条件

(distinct: 显示结果时,是否去除重复列 给哪一列去重就在哪一列字段前加入distinct)学生表

(1)查询表中的所有信息

SELECT * FROM student

(2)查询表中的所有学生姓名和对应的英语成绩

SELECT name,english FROM student

注:可显示部分字段,如果显示哪列数据,就直接写字段名称即可

(3) 过滤表中重复的math成绩

SELECT DISTINCT math FROM student;

(4) 创建一个student类 添加属性id,name,sex,chinese,English,math

并随机增加5条属性

select*fromstudent;–查询英语在70到75之间的学生的信息--select*fromstudentwhereenglishBETWEEN70AND75;–查询语文是80或者82或者90分的学生信息--select*fromstudentwherechineseIN(80,82,90);–查询所有首字母为l的学生的成绩--select*fromstudentwherenamelike"l%";–查询数学大于80且语文大于80的同学--select*fromstudentwheremath>80andchinese>90;–对数学成绩排序后输出(默认升序ASC)--select*fromstudentorderbymath;–对数学成绩排序后输出(降序DESC)--SELECT*FROMstudentorderbymathDESC;–指定多个字段进行排序,先按第一个字段进行排序,如果相同则按第二个字段进行排序--SELECT*FROMstudentORDERBYmathDESC,chineseDESC;–WHERE后可以加ORDERBY--SELECT*fromstudentwherenamelike"%l"ORDERBYmathDESC;–显示student表格中的前3行SELECT*fromstudentLIMIT2;–显示student表格中的第3~5行SELECT*fromstudentLIMIT2,3;--2表示偏移量,3表示显示的行数

附录:①在where中经常使用的运算符

注:逻辑运算符优先级 not>and>or

*②select|{column1|expression、column2|expression,…}fromtable;selectcolumnas别名fromtable;

注:

expression : mysql支持表达式 加减乘除;as: 表示给某一列起别名;并且as 可以省略;

– 关联(1对N)

createtablecustomer(idintPRIMARYKEYauto_increment,namevarchar(20)notnull,adressvarchar(20)notnull);createtableorders(order_numvarchar(20)PRIMARYKEY,priceFLOATnotNULL,customer_idint,--进行和customer关联的字段外键constraintcus_ord_fkforeignkey(customer_id)REFERENCEScustomer(id));insertintocustomer(name,adress)values("zs","北京");insertintocustomer(name,adress)values("ls","上海");SELECT*fromcustomer;INSERTINTOordersvalues("010",30.5,1);INSERTINTOordersvalues("011",60.5,2);INSERTINTOordersvalues("012",120.5,1);SELECT*fromorders;

主键和唯一标识

unique 唯一性标识

primarykey主键(auto_increment设置自动增长)--UNIQUE表约束唯一性标识--PRIMARYKEY主键CREATETABLEt4(idINTPRIMARYKEYauto_increment,NAMEVARCHAR(20)NOTNULL,genderCHAR(5)NOTNULL,idCardVARCHAR(20)UNIQUE--UNIQUE唯一性标识);desct4;insertintot4(name,gender,idCard)VALUE("zs","man","110");insertintot4(name,gender,idCard)VALUE("ls","woman","112");

关于SQL中如何使用DQL查询语言问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。