MySQL结构化查询,SQL结构化查询语言(二)
根據上一章節SQL結構化查詢語言(一),創建數據庫和數據表后,可以增加自己想要的數據:
https://www.jianshu.com/p/7aa34c91efa7
insert into my_contacts(last_name,first_name,email,gender,birthday,profession,location_1,status_1,interests,seeking) --1,insert into 往數據表中拆入數據
values('Bnderson','Jillian','jill_anderson@breakneckpizza.com','F','1980-09-05','Technical Writer','Palo Alto,CA','Singel','Kayaking,Reeptiles','Relationship,Friends1');
insert into my_contacts(last_name,first_name,email,gender,birthday,profession,location_1,status_1,interests,seeking) --1,insert into 往數據表中拆入數據
values('Cnderson','Jillian','jill_anderson@breakneckpizza.com','F','1980-09-05','Technical Writer','Palo Alto,CA','Singel','Kayaking,Reeptiles','Relationship,Friends2');
insert into my_contacts(last_name,first_name,email,gender,birthday,profession,location_1,status_1,interests,seeking) --1,insert into 往數據表中拆入數據
values('Dnderson','Jillian','jill_anderson@breakneckpizza.com','F','1980-09-05','Technical Writer','Palo Alto,CA','Singel','Kayaking,Reeptiles','Relationship,Friends3');
insert into my_contacts(last_name,first_name,email,gender,birthday,profession,location_1,status_1,interests,seeking) --1,insert into 往數據表中拆入數據
values('Enderson','Jillian','jill_anderson@breakneckpizza.com','F','1980-09-05','Technical Writer','Palo Alto,CA','Singel','Kayaking,Reeptiles','Relationship,Friends4');
insert into my_contacts(last_name,first_name,email,gender,birthday,profession,location_1,status_1,interests,seeking) --1,insert into 往數據表中拆入數據
values('Ender''s son','Jillian','jill_anderson@breakneckpizza.com','F','1980-09-05','Technical Writer','Palo Alto,CA','Singel','Kayaking,Reeptiles','Relationship,Friends4');
select * from my_contacts; -- 查看表中的詳細內容
插入數據.png
select * from my_contacts where last_name >= 'a%' and last_name < 'c%'
SELECT語句中可以使用WHERE進行條件選擇,在WHERE語句后面可以加上大于,等于,小于等限定條件。
select * from my_contacts where last_name >= 'a%' and last_name < 'c%'
SELECT選選項中可以選擇需要出現的列,這樣做的好處可以是加快查詢速度,讓SQL幫我們承擔過濾數據的重擔。
select last_name,first_name from my_contacts where last_name = 'Ender''s son';
在變量名稱中如果出現單引號(如,student's flowers),在進行匹對和篩查時可以使用轉義(',\),如student''s flowers,或student\'s flowers。
想要選擇所有的列,可以在SELECT中使用(*)。
篩選條件的關鍵字可以是:AND, OR, LIKE, NOT, BETWEEN, IN,IS NULL。
模糊匹配可以選擇LIKE,例如:
select last_name,first_name from my_contacts where last_name LIKE '%son';
(在單引號中放入百分比符號(%),就是告訴軟件,我們要在locaton列中查找所有以“son”結尾的值)
所以說,百分比(%)符號是任意數量的未知字符的替身。
第一個通配符是%,第二個通配符是下劃線( _ ),它是一個未知字符的替身。
select * from my_contacts where gender is null
如果數據表中有NULL也是可以像上面這句一樣,可以篩選出來。
當然,在條件篩查時,如果使用了where count >=30 AND count <=60,那么使用BETWEEN更好。BETWEEN不僅使查詢的長度比較短,而且能返回一樣的結果,可以改寫為:WHERE count BETWEEN 30 AND 60;
另外,我們在WHERE 語句中查找OR條件時(where last_name = 'Dnderson' or last_name = 'Enderson' OR ... )除了使用這么多的OR,我們也可以簡單地利用關鍵字IN,加上括號圍起來的值的集合。只要列值匹配集合中的任何值,即可返回該行或該列。
WHERE last_name IN ('Bnderson','Cnderson','Dnderson','Enderson');
select last_name from my_contacts where not (last_name = 'Dnderson' or last_name = 'Enderson')
也可以取相反的邏輯,NOT IN,可以反轉查詢結果,找出值不在集合中的記錄。
總結
1.SELECT * 用于選擇表中的所有列,用(‘)與(\)轉義,字符串中的單引號前應加上另一個單引號或反斜線來把它轉換成直接量。
2.掌握 = <> < > <= >=運算符,IS NULL可以用來NULL值條件。
3.AND與OR可以在WHERE子句中結合查詢條件,讓查詢更精確,NOT可以反轉查詢結果,取得相反的值。
4.BETWEEN選擇一個范圍內的值,LIKE搭配(%)或(_)通配符搜索部分文本字符串。
總結
以上是生活随笔為你收集整理的MySQL结构化查询,SQL结构化查询语言(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: inotifywait监听php,利用i
- 下一篇: python pymysql实例_pyt