零基础mysql项目实例_MySQL-零基础开发
1.終端下連接mysql服務(wù)
mysql -uroot -p回車后輸入設(shè)定的密碼即可。
進(jìn)去后每條命令結(jié)尾要帶分號(hào);退出命令exit
單行注釋有兩種:# 或 --空格。多行注釋/* */
2.基本命令集合
針對(duì)數(shù)據(jù)庫(kù):use sys; show databases;
查看當(dāng)前操作的數(shù)據(jù)庫(kù):select databse();
針對(duì)表:1.創(chuàng)建表eg1示例:create table? eg1( #此時(shí)回車
stuid int,
stuname varchar(20),
gender? char, #代表單個(gè)字符
borndate datetime); #命令結(jié)尾時(shí)才帶分號(hào)
2. desc eg1; #查看表的描述 describe
select * from eg1; #查看表中所有字段數(shù)據(jù),新創(chuàng)建的表應(yīng)返回為Empty set
insert into eg1 values(1,'張三','男','1999-6-6'); #向表中插入數(shù)據(jù),注意使用英文逗號(hào)
insert into eg1 values(2,'李四','男','1999-6-6');
#若提示格式不對(duì),set names utf8;修改my.ini里的utf8為gbk
update eg1 set borndate='2020-02-02' where stuid=2; #更新/修改表中的數(shù)據(jù),如果沒(méi)有后面的where,會(huì)更新整列數(shù)據(jù)delete from eg1 where stuid=1; #刪除數(shù)據(jù)alter table eg1 add column email varchar(20); #修改表的結(jié)構(gòu),添加列drop table eg1; #刪除整個(gè)表
3.基礎(chǔ)查詢
查詢結(jié)果是個(gè)虛擬表,不能直接操作數(shù)據(jù)。
select? ?#? 常量? 表達(dá)式(這兩個(gè)不用寫來(lái)自哪個(gè)表)? 函數(shù)?字段。對(duì)于來(lái)自哪個(gè)表可以雙擊表頭,將自動(dòng)用著重符·填寫。不是關(guān)鍵字的可以不加著重符號(hào)
F12鍵 可對(duì)齊命令。
select version() #查詢版本
select? user() #查詢用戶
起別名
select user() as 用戶名; #as 也可以省略為空格
select user() as '用? 戶名'; #包含了空格,避免使用查詢時(shí)出現(xiàn)語(yǔ)法錯(cuò)誤
select user() as "用戶? ?名";
例如:select last_name? as? "姓? ?名" from table; #若不使用引號(hào),會(huì)出現(xiàn)語(yǔ)法錯(cuò)誤。
+ 在mysql中作為運(yùn)算符時(shí),字符型強(qiáng)制轉(zhuǎn)換為整形失敗,則默認(rèn)為0。其中一個(gè)操作數(shù)為null時(shí),null+null=null=null+12=null。
字段拼接查詢
select concat(字段1,字段2) as "新? 字? 段" from? table;
去重查詢: select distict字段? from table;
顯示全部列,各個(gè)列用逗號(hào)連接,列頭顯示為out_put:
select concat(字段1,','字段2,','字段3) as 新字段 from table;
ifnull(表達(dá)式1,表達(dá)式2) #如果表達(dá)式1為null,顯示結(jié)果為表達(dá)式2.
避免查詢出null: select concat(字段1,','字段2,','ifnull(字段3,'')) as 新字段 from table;
4.條件查詢
select查詢列表from table
where 篩選條件;
執(zhí)行順序?yàn)?#xff1a;from->where->select
select * from table where id<>100; #查詢id不等于100的信息。
select * from table where not(id>=20 and id<= 60); #查詢id小于20大于60的信息。雖然可以用!代替not, &&代替and? 但是不建議,這樣不專業(yè)。
模糊查詢:like 一般和通配符_(單個(gè)字符) %(多個(gè)字符)
select * from table where like '%條件%' #查詢包含? 條件? 的信息。
查詢下劃線_: '$_%' escape '$'; #escape 使$符號(hào) 變?yōu)槭罐D(zhuǎn)義字符,相當(dāng)于\ 不過(guò)不建議,不炫。
in (常量表達(dá)式1,常量表達(dá)式2,常量表達(dá)式3) not? in 非數(shù)值的常量值,比如字符,要用單引號(hào)引起來(lái)。
select 字段 from table where id in(55,66,77); #查詢id 為 55? 66 ? 77
between? and #判斷某個(gè)字符的值是否介于xx之間。
select 字段 from table where id between 30 and 90; #
= #用于普通內(nèi)容
is null is not? null? #用于null
<=> #安全等于,既能判斷普通內(nèi)容,又能判斷null值
舉例:id<=> null; id<=>22;
5.排序查詢
select查詢列表from table
where 篩選條件
order by 排序列表
select? * from table where id>100 order by salary asc; #asc是升序,不寫默認(rèn)為升序。降序?yàn)閐esc。
select *,num*12 總額 from table where id is not null order by 總額 desc; #插入新算術(shù)表達(dá)式,并降序排列。
按函數(shù)的結(jié)果排序:
select 字段 from table order bylenth(字段); #按字段字節(jié)長(zhǎng)度升序排列
select 字段 from table order by char_lenth(字段); #按字符長(zhǎng)度排列
select字段1 字段2 字段3from table
order by 字段1,字段2 desc; #先按字段1升序排列,再滿足按字段2降序排列。
select * from table order by 字段; #字段為第2列時(shí),就寫個(gè) order by 2 也行。
6.函數(shù)
字符函數(shù):拼接字符:concat
select lenth('ab天'); #結(jié)果為5,一個(gè)漢字三個(gè)字節(jié)
select char_lenth('ab天') #結(jié)果為3。
截取字符:SELECT SUBSTR('因?yàn)樽约翰粔蛏车穸械阶员?#39;,7,2); #7為起始索引(從1開(kāi)始),2為長(zhǎng)度,輸出結(jié)果為 沙雕,不寫長(zhǎng)度截取到最后。
獲取字符出現(xiàn)索引:select instr('因?yàn)樽约翰粔蛏车穸械阶员?#39;,'自己'); #結(jié)果為3
去空格:默認(rèn)是去空格
select trim('x' from 'xxxxxx本品xxxx' ) as? a ;
SELECT TRIM( '? ?本品 ' ) AS? a ; #結(jié)果都是下圖
填充:左填充 lpad 右填充rpad
select lpad('木婉清',10,'a');
select rpad('木婉清',4,'a');
#列寬為1時(shí),就一個(gè)木 字。
總結(jié)
以上是生活随笔為你收集整理的零基础mysql项目实例_MySQL-零基础开发的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 购买新车时,应该考虑哪些因素?
- 下一篇: 扩张型心肌病预后差什么意思(扩张型心肌病