MySQL Select
查询分类
单表查询:简单查询
多表查询:连接查询
联合查询:多个查询结果汇总
查询的组成
投影查询:挑选要显示的字段
select array1,array2,... from tb_name;
选择查询:挑选符合条件的行
select * from tb_name where-clause
where-clause:布尔条件表达式= # 等值比较 # 等值比较,包括与NULL的安全比较或!= # 不等值比较,>= # 其它比较符IS NULL # 是否为空值IS NOT NULL # 是否不为空值LIKE # 支持的通配符有%和_RLIKE或REGEXP # 正则表达式匹配IN # 判指定字段的值是否在给定的列表中BETWEEN … AND …# 在某取值范围内
组合条件查询
NOT,!# 非AND,&& # 和OR,|| # 或
聚合函数查询
SUM() # 求总和AVG() # 求平均值MAX() # 求最大值MIN() # 求最小值COUNT() # 求记录总数#注:count(*)效率最低,可指定某一字段求总数,如count(Name)
查询语句
语法
select ... from tb_name where-clause [others-clause]
各子句类型及作用
where # 条件过滤group by # 对查询结果分组having # 对group by的结果进行条件过滤order by # 排序limit # 限制输出行数,如”limit 2“表示只显示前2行,“limit 2,3”表示偏移前2行,显示3-5行
其它修饰符
123 |
|
select的执行流程
from-->where-->group by-->having-->order by-->select-->limit |
多表查询
说明
事先将两张或多张表连接(join),根据连接的结果进行查询
语法
select … from tb1 Join_Type tb2 [on (Join_Condition)]连接类型有:cross join,inner join,left join和right join等
连接分类
cross join:交叉连接,又称笛卡尔乘积
表1中的记录(共M条)与表2中的记录(共N条)逐一连接,交叉连接数据量较大,共M*N条记录;
select * from students,classes# 注:不带where子句哦
inner join:内连接,根据连接判断的运算符又可分类如下:
等值连接:根据表1和表2中某字段值相等进行连接,其查询结果中列出被连接表中的所有列,包括其中的重复列
select * from students,classes# 注:不带where子句哦
条件比较连接:在连接条件中使用除等号(=)运算符之外的运算符,如>,>=,等
自然连接:在连接条件中使用等号(=)运算符,且只选择结果集中的部分列,并删除连接表中的重复列
select a.name,p.pub_name from authors as a inner join publishers as p on a.city=p.city
外连接:返回查询结果集合中的不仅包含符合连接条件的行,而且还包括左表(左外连接)、右表(右外连接)或两个边接表(全外连接)中的所有数据行
左外连接:返回包括左表中的所有记录和右表中联结字段相等的记录
tb1 LEFT JOIN tb2 ON 连接条件
右外连接:返回包括右表中的所有记录和左表中联结字段相等的记录
tb1 RIGHT JOIN tb2 ON 连接条件
全外连接:连接运算两边的关系中的元组都保留,MySQL不支持此种连接
自连接:同一表中某字段的多次比较使用
select stu1.Name as StuName,stu2.Name as TeachName from students as stu1,students as stu2 where stu1.TeacherID=stu2.StuID;
子查询
说明:
在外层查询中嵌套的内层查询叫做子查询,类型有:
where型子查询:把内层查询结果当做外层查询的比较条件
select goods_id,goods_name from goods where goods_id = (select max(goods_id) from goods)# 子查询的返回值只能有1个
exists型子查询:把内层查询结果拿到外层,看外层的查询是否成立
select cat_id,cat_name from category where exists(select * from goods where goods.cat_id = category.cat_id);# 可通过转换为内连接查询:select cat_id,cat_name from category inner join goods on goods.cat_id=category.cat_id;# 与exists类似的还有in子查询,表示外层查询条件在一个内层查询的结果集内select * from user_core where userId in (select userId from user_classes where class_id=’A001’);
from型子查询:把内层的查询结果供外层查询使用
select name,avg(score) from stu where name in (select name from (select name,count(*) as gk from stu having gk >=2)) group by name;
注:MySQL不擅长于子查询的优化,故应避免使用子查询
联合查询
说明
把两个或多个查询语句的结果合并成一个结果进行输出,前提是多个查询结果的输出字段是一致的
语法
SELECT clause UNION SELECT clause UNION …
union:会将多个表的查询结果合并后进行排序,同时删除重复的行,故效率略慢;union和union all的区别
union all:只是简单的将多个表的结果合并后返回,不删除重复行,效率较高
实例
涉及的表结构:
查询需求有:

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Win11 system comes with nearby sharing, casting, projection, and sharing functions. How to use these functions? what differences are there? Please see the introduction below for details. 1. Official documentation for nearby sharing: Nearby sharing - Microsoft Community shares content with nearby devices in Windows (microsoft.com) Function: Two computers transfer files via Bluetooth. (Can be used without WiFi or USB disk) Prerequisite: Two computers Steps: 1) Right click → Go to settings 2) System → Attach to anyone (to allow other devices to discover this device) 3) Both computers Set up like this 4) Right-click the file you want to send and click Share. 5) Click on another discovered computer to transfer files

When using our projection function, we can project the screen of the mobile phone onto the computer screen. We only need to connect the mobile phone and the computer through the network or Bluetooth, and then use the screen projection function on the computer. How to project win10 to this computer: 1. Go to settings from the start menu and select enter. 2. Then we can find the option in the coordinates and click to enter. 3. Then we set it up. 4. After the settings are completed, press the + shortcut key and then select. 5. Then we open the settings of the mobile phone. We can see that some mobile phones have more connection methods. Enter the settings. 6. We found the option and entered to select our own computer equipment. 7. At this time, a prompt box will appear on our computer, we just click it.

1. Connect the projector and iPhone to the same network, swipe down from the upper right corner of the screen, open the control center, and click [Screen Mirroring]. 2. In the scan results of screen mirroring, find the projector device for mirroring.

The win10 projector only displays the desktop background but not the desktop icons. What's your problem? I believe many friends have encountered this problem. In fact, this problem is easy to solve. Let's take a look with the editor. Solution to win10 projection not showing desktop icons: You have turned on the dual-screen extended mode. This is not a problem, it is just a display mode. That is to say, your current computer has two displays, one is the home screen of your computer. The other is the screen projected by your projector, and your current system has selected the dual-screen expansion mode by default. 1. We can press the "win+P" keys on the keyboard at the same time. 2. On one side of the home screen desktop (the pop-up method will be different depending on your system version) the following 4 options pop up. computer screen only

When using the projection function of win10, many users said that when using this function, the projection function did not respond or could not be used normally. At this time, it is necessary to check the system function first and then confirm the working condition of the projector. It can be solved. What to do if Windows 10 projection does not respond: 1. If you are projecting from a mobile phone to a computer, please make sure that your mobile phone and computer are on the same WiFi network. 2. In the settings on the computer, set it to on the right and turn it off. 3. If the problem still cannot be solved, please reinstall the network card driver, uninstall and reinstall, or update directly. 4. If you are connecting to a projector, please check whether there is a problem with the VGA cable and whether it is connected properly. 5. If the computer has dual systems, it will also cause graphics card driver conflicts.

When using a win10 projector, we need to open it from the settings or control panel every time. It is very troublesome to operate. So does the projector have shortcut keys? Of course there is, let’s take a look at the shortcut keys. Let’s introduce~ What are the shortcut keys for projection in Win10: 1. Users can quickly open the projection settings by pressing + on the keyboard. 2. Then we select the required method and then we can project. Related articles: How to project win10 to this computer>>>How to fill the full screen with win10 projector>>>

Win10 users generally like to use the projection function of Windows 10 system. If you find that the "Project to this computer" option is not displayed in the connection function menu. Don’t worry, here’s a detailed guide on how to easily fix this problem! Please follow the steps below: What to do if the option to project to this computer is no longer available in Windows 10 Home Edition 1. First, click on the dedicated icon in the lower left corner, and you will see multiple option menus pop up. 2. Click any option button again to confirm and enter the page. 3. On the home page, find and click the button and click the tab. 4. At this point, we will find that there are two different types of projection settings in this option. 5. To ensure that the screencasting function can be smoothly implemented on any occasion, just check it.

The projection function is used by more and more users, not only because the projection function allows the system software to share the screen, but also is very convenient to operate. Some users like to use the projection function for demonstrations. However, when some users use this function, they find that this function is grayed out and cannot be selected. Therefore, what can be done to solve the problem of graying out when projecting to this computer through win10? In fact, users can introduce how to solve the problem when projecting to this computer through win10. way to handle the situation. What to do if Windows 10 is grayed out when projecting to this computer? 1. Press the Win+i keyboard shortcut to open Windows Settings. You can search for settings in the input box, and you can also directly click on the system software (Display, Sound, Notification, Power). System software homepage setting dialog
