search
HomeDatabaseMysql TutorialMysql中的DQL查询语句_MySQL

bitsCN.com

----------------1、查询所有列 --查询 学生 表所有记录(行) select *from 学生

--带条件的查询 select *from 学生 where 年龄>19

-------------------2、查询指定的列 --查询 所有人的姓名和性别 select 姓名,性别 from 学生

--查询 所有 年龄>19 的学生的 姓名 select 姓名,地址 from 学生 where 年龄>19

/*比较运算符 = > = 不等于 !>不大于 !

--方式二 select 姓名,地址as家乡 from 学生

-----------------------4、消除重复 --查询该表有哪些家乡 select distinct 地址 from 学生

----------------------5、top n(查询前N条) select top 3 * from 学生 --查询前3条记录

-----------------------6、排序 select *from 学生 order by 年龄 asc     --按年龄进行升序排列                        --desc降序         --asc升序

select * from  学生 order by 年龄 desc ,编号 asc ---按年龄降序 --先按年龄进行降序,在出现相同年龄的时候,把这些相同的学生 再按照 学号 升序排列

--例:查询 学生 表中,年龄最大的三个学生的 年龄、姓名、编号 select top 3  年龄,姓名,编号 from 学生 order by 年龄 desc

-------------------------------7、 and(并且)、or(或者) select *from 学生 where 年龄=20 and 姓名='张三'

--例如:查询 性别为男的 或 专 地址为武汉 select *from 学生 where 性别='男'  or 地址='武汉'

----------------------8、between ... and(介于...之间) --例:查询年龄为20-30之间的所有人 select *from 学生 where 年龄 between 20 and 30

-----------------------9、in 的用法 select * from 学生 where 年龄 in(20,19,18)

---------------------------10、top N  与 order by 同时使用

--例:查询年龄最大的一人 select top 1 with ties * from  学生  --加了with ties 后 如有并列第一的就全都显示 order by 年龄 desc

---------------------------------11、case替换查询结果 --查询所有人信息,如果年龄>=40岁,就显示"中年人", --     如果年龄 介于30-39  ,就显示“青年” --     如果年龄  介于20-29 ,就显示“青少年” --     如果年龄   小于20  , 就显示“少年”

select 学号,姓名,性别, 年龄=case   when 年龄>=40 then '中年人'   when 年龄 between 30 and 39 then '青年'   when 年龄 between 20 and 29  then '青少年'   else '少年' --else表示不满足以上条件时,就全部  end ,住址 from 学生

-----------------------------------12、模糊查找 使用like子句进行模糊查询 like子句与通配符配合使.Sql server提供4种通配符 1.%:表示任意字符 2. _:表示单个任意字符 3.[ ]:表示方括号里列出的任意一个字符. 4.[^]:表示任意一个没有在方括号里列出的字符.

--例:查找姓周的所有人信息 select * from 学生 where 姓名 like '周%' --%可以代替任意几个字符

select * from 学生 where 姓名 like '周_' --_表示可以代替一个字符

--例:查找姓名的第二个字包含 ’心‘ 或者 ’三‘ 的人 select * from 学生 where 姓名 like '_[星,三]_'

 

--嵌套查询(一般嵌套请不要超过3层,即不要出现超过3个select) select * from 学生  where 年龄

  --例如:查询所有比 中文系所有学生年龄 大的学生 select * from xs where 年龄> (  select top 1 年龄 from xs  where 所在系='中文'  order by 年龄 desc )

/*运算符  all some   any  */ 

/* all:指定表达式要与子查询结果集中的每个值都进行比较,当表达式与每个值都满足比较的关系时,才返回true,否则返回false;

Some和any:表示表达式只要与子查询结果集中的某个值满足比较的关系时, 就返回true,否则返回false.

*/

select * from xs where 年龄>all (  select 年龄 from xs where 所在系='中文' )

----------------------------------表的复制 /*把所有计算机系的学生拉出来单独创建一个表*/

create  table  xs_jisuanji    --创建一个新表 (  学号 int,  姓名 varchar(50),  性别 char(10),  年龄 int )

insert into xs_jisuanji       --查询内容 并复制内容到新建的表 select 学号 ,姓名, 性别, 年龄 from xs where  所在系='计算机'H

 

/*复制方式二*/   --创建中文系的表 select 学号,姓名,性别 ,年龄 into xs_zhongwen from xs where 所在系='中文'

---跨数据库表的复制(需要在 表名前加数据库名) select  * into test.dbo.xs  from   n2d09003

 

 

-------------------------------------------- --聚合函数

--求学生总分 select sum(成绩) as 总分数 from xs

--求分数最高分 select max(成绩) as 最高分 from xs

-- 求最低分 select min(成绩) as 最低分 from xs

--求平均分 select avg(成绩) as 平均分 from xs

--统计有多少名学生 select count(成绩) as 人数 from xs

---------------------------------------分类汇总 group by --例1

--查询学生表中有哪些专业 select distinct 所在系 from xs

--group by 实现 select 家乡 from N2D09003 group by 家乡

--例2 求每个地方的学生数 select 家乡,count (*) as 人数 from N2D09003 group by 家乡  --按照家乡  进行分类汇总

--[例3]求每个地方 男生和女生的人数 select 家乡,性别,count(*) as 人数 from N2D09003 group by 家乡,性别  --按照家乡 和  性别 进行分类汇总

/*` [特别注意:1:select 后面出现的列名,必须出现在group by 后面] 2:group by与order by连用,order by 子句中可包含聚合函数. 3、group by关键字后可以使用多个字段名作为分组字段,这样, 系统将根据这些字段的先后顺序对结果集进行更加详细地分组。

--[例4]求每个地方的总人数,并且按照人数从多到少排序 select 家乡,count(*) as 人数 from N2D09003 group by 家乡 order by 人数 desc    --这里的order by 后面可以是聚合函数(如果需要的话)

select * from xs order by max(年龄) dese --错误  不满足使用要求第二条

--------------------------------------------------------------09.12.04

-------------------------------------group by ...having --作用:分类汇总后,再进行筛选 /*查询每个专业总人数,并且显示 总人数>3人的专业*/ select 所在系 , count(*) as  人数 from  xs group  by 所在系 having count(*)>3  --筛选出人数>3人的专业

--------------------group by ....with rollup select 所在系, count(*) as 人数 from xs group by 所在系,性别 with rollup  --在分类汇总之后,再次汇总

select 所在系,性别, count(*) as 人数 from xs group  by 所在系,性别 with rollup  --在分类汇总之后,再次汇总

-------------------group by .... with cube select 所在系,性别,count(*) as 人数 from xs group by 所在系,性别 with cube --比rollup 汇总的更详细(按照 group by 后面的列进行再次汇总)

------------------------------------------------链接查询 /*查找选修了课程号为2且成绩在80分以上的学生姓名和成绩*/ select 姓名,xx.成绩 from xs,xx where xs.学号=xx.学号  --两表链接条件 and 课程号=2 and xx.成绩>80

--加了 表名.列名 (一般无需在列名之前加表名前缀,只有当两个表有相同的列名时才加前缀) select xs.姓名,xx.成绩 from xs,xx where xs.学号=xx.学号  --两表链接条件 and xx.课程号=2 and xx.成绩>80

-----------------------查询  刘德华的成绩 --方式一 省略前缀 select xx.成绩 from xx,xs        where  xx.学号=xs.学号 and 姓名='刘德华'

--方式二 嵌套查询 select 成绩 from xx where 学号=    ( select 学号 from xs where 姓名='刘德华' )

--方式三 内联式查询 select  xx.成绩 from xx            join xs on  xs.学号=xx.学号     --两表连接条件 where 姓名='刘德华'             --其他限制条件

--查询林心如的古汉语成绩 select 姓名, 课程名 ,xx.成绩

from xs  join  xx on  xs.学号=xx.学号          join  kc on  kc.课程号=xx.课程号 and 姓名='林心如' and 课程名='古汉语'

select 姓名 ,课程名 ,xx.成绩 from  xs,xx,kc where xs.学号=xx.学号 and xx.课程号=kc.课程号 and 姓名='林心如' and 课程名='古汉语'

select 成绩 from xx where 课程号= ( select 课程号 from kc where 课程名='古汉语' ) and 学号= ( select 学号 from xs where 姓名='林心如' )

bitsCN.com
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL String Types: Storage, Performance, and Best PracticesMySQL String Types: Storage, Performance, and Best PracticesMay 10, 2025 am 12:02 AM

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

Understanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreUnderstanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreMay 10, 2025 am 12:02 AM

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

What are the String Data Types in MySQL?What are the String Data Types in MySQL?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software