与MSSQL对比学习MYSQL的心得(七)--查询

论坛 期权论坛     
niminba   2021-5-22 22:26   48   0
<p>比较出大家的异同点,从而加深记忆</p>
<p>这一篇《与MSSQL对比学习MYSQL的心得(七)》也是一样,相同的地方略略带过,不同的地方我会给出例子,谢谢大家的支持o(∩_∩)o</p>
<p>这一节主要介绍MYSQL里的基本查询(MYSQL官方参考手册)</p>
<p>MySQL中select的基本语法形式:</p>
<p>select 属性列表<br>
from 表名和视图列表<br>
[where 条件表达式]<br>
[group by 属性名[having 条件表达式]]<br>
[order by 属性名[asc|desc]]<br>
[limit &lt;offset&gt;,row count]<br>
&nbsp;<br>
说明:</p>
<p>where子句:按照“条件表达式”指定的条件进行查询。</p>
<p>group by子句:按照“属性名”指定的字段进行分组。</p>
<p>having子句:有group by才能having子句,只有满足“条件表达式”中指定的条件的才能够输出。</p>
<p>group by子句通常和count()、sum()等聚合函数一起使用。</p>
<p>order by子句:按照“属性名”指定的字段进行排序。排序方式由“asc”和“desc”两个参数指出,默认是按照“asc”来排序,即升序。</p>
<p>建立测试表</p>
<p>创建测试表</p>
<div class="blockcode">
<pre class="brush:sql;">
CREATE TABLE fruits
(
f_id CHAR(10) NOT NULL,
s_id INT NOT NULL,
f_name CHAR(255) NOT NULL,
f_price DECIMAL(8,2) NOT NULL,
PRIMARY KEY(f_id)
)
</pre>
</div>
<p>插入测试数据</p>
<div class="blockcode">
<pre class="brush:sql;">
INSERT INTO fruits(f_id,s_id,f_name,f_price)
VALUES('a1',101,'apple',5.2),
('b1',102,'blackberry',5.2),
('bs1',105,'orange',5.2),
('bs2',103,'melon',5.2),
('t1',106,'banana',5.2);
</pre>
</div>
<p>使用select语句查询f_id字段的数据</p>
<p>SELECT f_id,f_name FROM fruits</p>
<p><img alt="" src="https://beijingoptbbs.oss-cn-hangzhou.aliyuncs.com/jb/2426819-1beed99791c237ba6bcf2f185ec1934a.jpg"><br>
&nbsp;<br>
注意:MYSQL中SQL语句是不区分大小写的,因此select和SELECT作用是相同的</p>
<p>这一点跟SQLSERVER是一样的!</p>
<p>常用查询</p>
<div class="blockcode">
<pre class="brush:sql;">
SELECT * FROM fruits
SELECT f_id,f_name FROM fruits WHERE f_price &gt;5.1
SELECT f_id,f_name FROM fruits WHERE s_id IN(101,102)
SELECT f_id,f_name FROM fruits WHERE s_id NOT IN(101,102)
SELECT f_id,f_name FROM fruits WHERE f_price BETWEEN 2 AND 10
SELECT f_id,f_name FROM fruits WHERE f_price NOT BETWEEN 2 AND 10
</pre>
</div>
<p>&nbsp;<br>
带like的字符匹配查询</p>
<p>1、百分号通配符“%”,匹配任意长度的字符,甚至包括零字符</p>
<div class="blockcode">
<pre class="brush:sql;">
SELECT f_id,f_name FROM fruits WHERE f_name LIKE 'b%y'

</pre>
</div>
<p><img alt="" src="https://beijingoptbbs.oss-cn-hangzhou.aliyuncs.com/jb/2426819-43f0f2d6cbcbea8b97b788478b9cc5bd.jpg"></p>
<p>&nbsp;2、下划线通配符“_”,一次只能匹配任意一个字符</p>
<p>下面语句有四个下划线</p>
<div class="blockcode">
<pre class="brush:sql;">
SELECT f_id,f_name FROM fruits WHERE f_name LIKE '____n'

</pre>
</div>
<p><img alt="" src="https://beijingoptbbs.oss-cn-hangzhou.aliyuncs.com/jb/2426819-d36441af8be1ed20b8ff34838fc3fabf.jpg"></p>
<p>同样,在SQLSERVER里面也是有的</p>
<div class="blockcode">
<pre class="brush:sql;">
USE [sss]
GO
SELECT * FROM [dbo].[aaa] WHERE [name] LIKE 's_____'

</pre>
</div>
<p><br>
&nbsp;<img alt="" src="https://beijingoptbbs.oss-cn-hangzhou.aliyuncs.com/jb/2426819-3c175b78961247c266a3b6363b017035.jpg"><br>
查询空值</p>
<div class="blockcode">
<pre class="brush:sql;">
CREATE TABLE customers
(
c_id INT NOT NULL AUTO_INCREMENT,
c_name CHAR(25) NOT NULL,
c_city CHAR(50)  NULL,
PRIMARY KEY(c_id)
)
INSERT INTO customers(c_name,c_city)
VALUES('liming','china'),
('hongfang',NULL)
SELECT * FROM customers WHERE c_city IS NULL

</pre>
</div>
<p><img alt="" src="https://beijingoptbbs.oss-cn-hangzhou.aliyuncs.com/jb/2426819-fd6d444c5ea4555a8fba1b30ee317aa2.jpg"></p>
<div class="blockcode">
<pre class="brush:sql;">
SELECT * FROM customers WHERE c_city IS NOT NULL

</pre>
</div>
<p><img alt="" src="https://beijingoptbbs.oss-cn-hangzhou.aliyuncs.com/jb/2426819-a7f22def8886c83d620a21ca7f635b5b.jpg"></p>
<p>AND、OR、DISTINCT关键字</p>
<div class="blockcode">
<pre class="brush:sql;">
SELECT f_id,f_name FROM fruits WHERE f_name LIKE '____n' AND f_id='bs2'
SELECT f_id,f_name FROM fruits WHERE f_name LIKE '____n' OR f_id='bs2'
SELECT DISTINCT s_id FROM fruits
</pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;<br>
GROUP BY</p>
<div class="blockcode">
<pre class="brush:sql;">
SELECT s_id ,COUNT(1) AS total FROM fruits GROUP BY s_id

</pre>
</div>
<p><img alt="" src="https://beijingoptbbs.oss-cn-hangzhou.aliyuncs.com/jb/2426819-199162e2df15de5f21d1df1853cbb761.jpg"></p>
<p>再插入两条记录</p>
<div class="blockcode">
<pre class="brush:sql;">
INSERT INTO fruits(f_id,s_id,f_name,f_price)
VALUES('a6',101,'cherry',6),
('a8',102,'coconut',7)
</pre>
</div>
<p>&nbsp;<br>
如果要查看每个供应商提供的水果的种类的名称,MYSQL中可以在GROUP BY中使用GROUP_CONCAT()函数,</p>
<p>将每个分组中各个字段的值显示出来</p>
<div class="blockcode">
<pre class="brush:sql;">
SELECT s_id,GROUP_CONCAT(f_name) AS NAMES FROM fruits GROUP BY s_id
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP