bitsCN.com
Sql语句
一般顺序GHOL : group by,having ,order by,limit
如果是分组,应该使用对分组字段进行排序的group by语法
Limit start ,length
去除重复记录默认为all
Select distinct 字段 from
Select distinct * from 没用(所有字段组合不相同才认为不相同,用在这里基本没用),记录值完全一样时取其一个
Union查询
把两个select 结果union起来
( select 语句1)union(select 语句2)
选出英语最高分和数学最高分的学生的id,name ,class
注意加括号
有重复记录时的合并
按english由高到低和由低到高的结果合并
在符合语句中 order by功能受影响,需加上limit
子语句的排序
1.将子语句包裹在子括号内
2.子语句的 order by
中有order by配合Limit使用时才生效。
原因是:union在做子语句时,会对没有limit的order by优化(忽略)
所有结果排序
只需要在最后一个select语句后增加相应排序即可。
子语句括号非必须最后一个排序默认针对所有结果。
Union检索的字段必须个数一样(否者出错),数据类型也一样(发生类型转换) 列名由第一个select检索列名来定
子查询
语句内部的查询语句
表中数据
查出英语成绩最高的学生的信息
不用子查询:
但有2个学生英语成绩最高且一致,这个时候在不知道表里数据的情况下就不能使用这种查询方法
思路:先找出英语成绩最大的那个值,再找出哪些学生的英语成绩与这个最大的值相等。这样一步一步进行查询。
只检索一个字段时可以作为一个值使用,必须只检索一个字段
子查询分类
不同的分类会有不同的使用方式
分类标准:
子查询出现的位置
子查询的返回值形式
返回值分类:
单一值,单列,多列,多行多列(表)
出现位置:
Where 型,where 后
From型 from后
Exists型
使用:
标量的:获得一个值后用关系运算符进行运算(> >=, )
列子查询(只是1列):获得一列通常是对个行的一列值(一个集合)
使用in,not in运算符
查出班级为php101中所有学生的信息
集合操作符还有
Any(集合) 集合中的任意一个
=any(集合) 等于集合中的任意一个即可
等同于in
=All(集合) 集合中的所有元素
!=all(集合) 不等于集合中的所有元素等同于Not in
!=any(集合) 不等于集合中的任意一个元素成立即可,即为只要不等于其中的一个元素即为成立的。
注意:这种语法不该出现在实际开发中
Some(集合) 集合中的一些
语法上与any一样
总结:
=any 等同于in
!=all 等同于 not in
Some 和any同义
All,any ,some可以使用除了=,!=之外运算符,比in强大
返回一行
在参与比较时,使用括号可以构建一行
(field1,field2,…)
表中数据
现在要查询出和贺8在同样的班级且与他math成绩一样的同学的信息
子查询
返回一个表
如果用在from子句内,要求要是一个表
现在是查询结果,必须给这个查询结果起别名
表中数据
查询php103班 english不及格的学生信息
必须有别名
Exists
如果子查询可以返回数据则返回真,否者返回假
有以下2表
A表
B表
在a中查询出id在 b中有的记录
先获取a表的第一行记录,在子查询中判断a表的id与b表的id比较
连接查询
Join
每个实体,一个表
一个业务逻辑,使用多个实体的数据
多张表应该在一起使用,将多个表的记录连接起来
Teacher表
Teacher_class1表
查出代课老师的代课信息
笛卡尔(交叉)连接
内连接处理
在连接时,是可以省略连接条件的。意味着,所有的左表数据,都要与右表的记录做一个连接
共存在m*n个连接
称之为交叉连接或笛卡尔集
此时可以使用 cross join代替inner join
Mysql中cross join与inner join相同
Inner join是默认的连接方式(inner 省略)
等效的
也可使用
表示笛卡尔积
结果虽然一样
On数据连接条件
Where数据过滤条件
但 where是先连接成笛卡尔积
然后做筛选,而on 是在连接时就判断
上表是连接条件2个
下表是
过滤条件2个
下表连接过滤各一个
Using:要求负责连接的两个实体之间字段名称一致。
查询条件与外连接通用 外连接不能用where作为连接条件。
注意:
无论是连接条件还是连接查询多字段列表,都没必要一定要写表名.字段语法,是否写取决于是否发生冲突。建议写上。
别名
表应该别名,保证简洁清晰。
列别名
外连接:
分类
左外连接
右外连接
全外连接(暂不支持)
左连接
在连接时,如果出现左边表,数据连接不到右边表的情况,则左表的数据在最终结果内保留。而如果出现右表的数据连接不到左标的情况,右表数据被丢弃。
由于内连接没有左右连接之分,left outer join中outer可以省略。
在外连接中不可以用where做连接条件可用on ,using
表别名可以用在连接条件里,但字段别名不可以。
表起别名后,在筛选或者连接条件里必须用别名,原名不能用了
左表teacher里的数据孙武连接不上也保留。
全外连接左外与右外 union(取并集)
内连接是左外右外交集
Using会去掉结果中重复字段,并放在列首
外连接不能使用没有条件的连接(不像内连接那样形成笛卡尔积)
自然连接
通过mysql自己判断完成连接过程。不需要指定连接条件,mysql会使用多表内相同的字段作为连接条件。
表one数据
Two表数据
自然连接也有内外之分
内:natural join
外:左外natural left join ,右外 natural right join

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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

SublimeText3 Chinese version
Chinese version, very easy to use

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.

Dreamweaver Mac version
Visual web development tools
