mysql between and 包含边界吗_MySQL | SQL语法(一)
本篇使用的數(shù)據(jù)庫管理工具是Navicat for MySQL,回復(fù)公眾號【SQL】可獲取本篇使用的3個(gè)數(shù)據(jù)源表。
本篇以3個(gè)數(shù)據(jù)源表table1,table2,table3為例,簡單舉例陳述SQL中數(shù)據(jù)查詢語言的使用。
01DB、DBMS與SQLDB(Database),即數(shù)據(jù)庫,相當(dāng)于一個(gè)倉庫,用于有組織地存儲數(shù)據(jù)。
DBMS(Database Management System),即數(shù)據(jù)庫管理系統(tǒng),用于操作和管理數(shù)據(jù)庫。主要分為兩大類:RDBMS、NoSQL。
RDBMS:關(guān)系型數(shù)據(jù)庫管理系統(tǒng),主要實(shí)現(xiàn)對結(jié)構(gòu)化數(shù)據(jù)的管理,為二元關(guān)系模型。如Oracle、MySQL、SQL Server、DB2等。
NoSQL:非關(guān)系型數(shù)據(jù)庫管理系統(tǒng),彌補(bǔ)關(guān)系型數(shù)據(jù)庫管理系統(tǒng)的不足。如Redis、MongoDB等。
SQL(Structured Query Language),即結(jié)構(gòu)化查詢語言,是關(guān)系型數(shù)據(jù)的一門通用語言,用于實(shí)現(xiàn)對數(shù)據(jù)庫的查詢、更新和管理。其語言主要分為四個(gè)部分:DDL、DML、DCL和DQL。
DDL(Data Definition Language),數(shù)據(jù)定義語言,它用來定義我們的數(shù)據(jù)庫對象,包括數(shù)據(jù)庫、數(shù)據(jù)表和列。通過使用DDL,我們可以創(chuàng)建、刪除和修改數(shù)據(jù)庫及表結(jié)構(gòu)。
DML(Data Manipulation Language),數(shù)據(jù)操作語言,我們用它操作和數(shù)據(jù)庫相關(guān)的記錄,比如增加、刪除、修改數(shù)據(jù)表中的數(shù)據(jù)。
DCL(Data Control Language),數(shù)據(jù)控制語言,我們用它來定義訪問權(quán)限和安全級別。
DQL(Data Query Language),數(shù)據(jù)查詢語言,我們絕大多數(shù)情況下都是在和查詢打交道,因此學(xué)會編寫正確且高效的查詢語句非常重要。
ROE均值?>?30?and?
毛利率均值?>40?and?
凈利潤現(xiàn)金含量均值?>?100?and?
資產(chǎn)負(fù)債率均值?<60?;Navicat for MySQL運(yùn)行結(jié)果如下:要全部符合and連接的條件才會被篩選出來,結(jié)果顯示只有5條數(shù)據(jù)符合條件。5.按條件查詢表中的數(shù)據(jù)(組合or)select * from table_name where column1 運(yùn)算符 value1 or column2 運(yùn)算符 value2select?*?from?table1?where?
股票簡稱='貴州茅臺'?or?
股票簡稱='海天味業(yè)';
Navicat for MySQL運(yùn)行結(jié)果如下:符合or連接中的任意一個(gè)條件即可以被篩選出來,結(jié)果顯示了指定的兩條數(shù)據(jù)。6.按條件查詢表中的數(shù)據(jù)(范圍between)select * from table_name where column between value1 and value2select?*?from?table1?where?
ROE均值?between?30?and?35;Navicat for MySQL運(yùn)行結(jié)果如下:在MySQL中,between包含value1和value2邊界值,結(jié)果顯示了有5條數(shù)據(jù)在指定的范圍內(nèi)。7.按條件查詢表中的數(shù)據(jù)(集合查詢in)select * from table_name where column in (value1,value2,...)select?*?from?table1?where?
上市年數(shù)?in?('3年','4年','5年');
Navicat for MySQL運(yùn)行結(jié)果如下:只要符合in后面中的任意條件就會被篩選出來,結(jié)果顯示有19條數(shù)據(jù)符合括號內(nèi)的條件。8.查詢并定義別名(as)as可省略
select column1 as name1,column2 as name2,... from table_name;
select column1,column2 from table_name as name
Navicat for MySQL運(yùn)行結(jié)果如下:
在table1表中有很多“上市年數(shù)”相同的數(shù)據(jù),distinct可以去除列中重復(fù)的數(shù)值并將其展示出來。10.查詢并進(jìn)行空值篩選(null)select * from table_name where column is null
select * from table_name where column is not null
Navicat for MySQL運(yùn)行界面如圖所示:
null即是空值,如果想篩選非空值使用not null即可。11.模糊查詢(like)1)查詢以s開頭的字段select * from table_name where column like 's%'2)查詢以s結(jié)尾的字段select * from table_name where column like '%s'3)查詢包含s的字段select * from table_name where column like '%s%'4)查詢第二個(gè)字是s的字段select * from table_name where column like '_s%'select?*?from?table1?where?股票簡稱?like?'%新材';Navicat for MySQL運(yùn)行結(jié)果如下:%指代任意字符,_代表單個(gè)字符。12.排序查詢(order by)select * from table_name order by column1 asc|desc,column2 asc|desc,...select?*?from?table1?order?by?ROE均值?desc;Navicat for MySQL運(yùn)行結(jié)果如下:asc代表升序排列,desc代表降序排列,默認(rèn)為升序排列。13.限制查詢(limit)select * from table_name limit index,linesselect?*?from?table1?limit?5;
Navicat for MySQL運(yùn)行結(jié)果如下:限制查詢可以按自定義查詢要展示的行數(shù)據(jù),lines代表要展示的行數(shù),index代表要展示的行數(shù)從第幾行開始算起,默認(rèn)從0開始,即從首行數(shù)據(jù)開始展示。14.分組查詢(group by)select avg(column1) from table_name group by column2select?上市年數(shù),avg(ROE均值)?from?table1?group?by?
上市年數(shù);Navicat for MySQL運(yùn)行結(jié)果如下:group by按照列中相同的數(shù)據(jù)分為一組,接著對分組后的數(shù)值進(jìn)行計(jì)算并展示。15.分組過濾查詢(having)select avg(column1) from table_name group by column2 having avg(column1) 運(yùn)算符 valueselect?上市年數(shù),avg(ROE均值)?from?table1?group?by?
上市年數(shù)?having?avg(ROE均值)>30;
Navicat for MySQL運(yùn)行結(jié)果如下:having的意思相當(dāng)于where,在這里使用having是因?yàn)閣here無法對分組后的數(shù)據(jù)進(jìn)行篩選(where的順序在group by前面)。16.關(guān)聯(lián)查詢(join)1)內(nèi)連接:通過同一個(gè)列中相同的值將表匹配起來,進(jìn)行關(guān)聯(lián)并展示。①使用條件where:該方法與inner join ... on ...結(jié)果相同select *?from table_name1,table_name2?where?table_name1.column = table_name2?.column
②inner join ... on ...
select * from table_name1?
inner join?
table_name2?
on?
table_name1.column = table_name2.column
select?*?from?table1,table2?where?table1.股票代碼=table2.股票代碼;
或
select?*?from?table1?inner?join?table2?on?
table1.股票代碼=table2.股票代碼;
Navicat for MySQL運(yùn)行結(jié)果如下:
table1有35條數(shù)據(jù),table2有31條數(shù)據(jù),兩表中“股票代碼”列相同的值只有31條,所以最后展示出來的只有31條數(shù)據(jù)。使用條件where和inner join ... on ... 結(jié)果相同,只不過最終會顯示兩個(gè)相同的列,若是想要合并該列,可使用以下第3種方法。
③合并列:inner join ... using ...
select * from table_name1?
inner join?
table_name2?
using(column)
select?*?from?table1inner?join?table2?using(股票代碼);
Navicat for MySQL運(yùn)行結(jié)果如下:
內(nèi)連接的方法只能將列中相同的值的行數(shù)據(jù)合并在一起,如果想要合并更多的行數(shù)據(jù),可以使用外連接。
2)外連接:以其中一張表為驅(qū)動(dòng)表,與另外一張表的每條記錄進(jìn)行匹配,如果能夠匹配則進(jìn)行關(guān)聯(lián)并展示,如果不能匹配則以null填充。
①左外連接:left join ... on
select * from table_name1?
left join?
table_name2?
on?
table_name1.column = table_name2.column
select?*?from?table1?left?join?table2?on?
table1.股票代碼=table2.股票代碼;
Navicat for MySQL運(yùn)行結(jié)果如下:
以table1為驅(qū)動(dòng)表,table1有35條數(shù)據(jù),table2有31條數(shù)據(jù),table1最后幾條比table2多的數(shù)據(jù)的剩余值將以null進(jìn)行填充。
②右外連接:right join ... on
select * from table_name1?
right join
table_name2?
on?
table_name1.column = table_name2.column
select?*?from?table1?right?join?table2?on?
table1.股票代碼=table2.股票代碼;
Navicat for?MySQL運(yùn)行結(jié)果如下:
以table2為驅(qū)動(dòng)表,table1有35條數(shù)據(jù),table2有31條數(shù)據(jù),結(jié)果以table2為準(zhǔn)只查詢出31條數(shù)據(jù)。
3)自連接
select * from table_name1 name1?
left join?
table_name1 name2?
on?
name1.column1 = name2.column2
與外連接同理,在同一張表中進(jìn)行匹配,如果能夠匹配的則進(jìn)行關(guān)聯(lián)并展示,不能匹配的則以null填充。
17.組合查詢(union)1)去除重復(fù)值
select * from table_name1?
union?
select?*?from table_name2
select?*?from?table1?union?
select?*?from?table3;
Navicat for MySQL運(yùn)行結(jié)果如下:
table1有35條數(shù)據(jù),table2在table1的基礎(chǔ)上增加了16條共51條數(shù)據(jù),使用union將兩張表進(jìn)行組合會去除其重復(fù)值,最后只顯示51條數(shù)據(jù)。
2)保留重復(fù)值:
select * from table_name1?
union all?
select *?from table_name2
select?*?from?table1union?all?
select?*?from?table3;
Navicat for MySQL運(yùn)行結(jié)果如下:
union all保留了兩張表中共有的數(shù)據(jù),所以最后顯示有86條數(shù)據(jù)。
在看點(diǎn)這里總結(jié)
以上是生活随笔為你收集整理的mysql between and 包含边界吗_MySQL | SQL语法(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: set集合判断集合中是否有无元素_集合
- 下一篇: linux c 读写mbr_一文看懂Li