mysql(四)-增刪改查

來源:互聯網
上載者:User

標籤:mysql

INSERT

一次插入一行或多行資料

文法:INSERT into [(欄位1,欄位2...)] VALUES (欄位1值,欄位2值...), (val21,...)INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);如果有自動遞增屬性auto_increment,會以新插入的自動遞增項最後以起始。http://dev.mysql.com/doc/refman/5.5/en/insert.html
inser into test1 (id,name,sex) values (1,‘zhangshan‘,‘M‘);欄位與值要一一對應向表tb1中插入多條資料,具體含義同上,只不過是插入多條語句insert into tb1 (name,age) values(‘jerry‘,22),(‘naruto‘,28);也可以不指定欄位,表示對應每個欄位都會有插入的資料。insert into tb1 values (4,‘Sasuke‘,28),(5,‘hinata‘,25);
UPDATE

修改行資料

注意:一定要有限制條件,否則將修改所有行的指定欄位,會出生產事故的。

但是也有方法規避

mysql 用戶端啟動時 增加參數 --safe-updates 或 -U ,當然,也可以寫到[client]
使用限制條件WHERELIMIT
update test1 set sex=‘F‘ where sex=‘M‘;

全表更新

update test1 set sex=‘M‘;ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

加入limit的更新

update test1 set sex=‘M‘ limit 1;Query OK, 1 row affected (0.52 sec)

加入where的更新

update test1 set sex=‘M‘ where id=2;Query OK, 1 row affected (0.01 sec)
DELETE

可先排序再指定刪除的行數

ORDER BY ...

限制行數

LIMIT
delete from test1 where id =1;從tb1表中找出age>30的資料行,然後將這些行按照age進行降序排列,排列後刪除第一個delete from tb1 where age > 30 order by age desc limit 1;

?注意:一定要有限制條件,否則將清空表中的所有資料

SELECT使用別名
欄位 as 欄位別名
WHERE子句:指明過濾條件以實現“選擇”的功能:
過濾條件:布爾型運算式算術操作符:+, -, *, /, %比較操作符:=, !=, <>, <=>, >, >=, <, <=BETWEEN min_num AND max_numIN (列表)    從tb1表中尋找出age等於22、23、24或25中的任意一個的行的所有資料    select * from tb1 where age in (22,23,24,25);NOT IN IS NULLIS NOT NULL
LIKE 與 RLIKE:
%: 任意長度的任一字元_:任意單個字元RLIKE:Regex,索引無效,不建議使用    select * from tb1 where name rlike ‘^t.*‘;REGEXP:匹配字串可用Regex書寫入模式,同上
邏輯操作符
NOTANDORXOR
order by 根據指定的欄位對查詢結果進行排序
升序:ASC   預設降序:DESCselect name,code2,indepyear from country where indepyear NOT in (1990,1800,1993) order by indepyear asc;如果多行之間的age欄位的值相同時,這些行再根據name欄位進行升序排序select * from tb1 order by age desc,name asc;把NULL排序到最後,在欄位名前用-,排序方法descorder by -indepyear desc
DISTINCT 去重查詢

查詢某欄位的時候去重,使用DISTINCT關鍵字表示去重查詢

select distinct indepyear from country order by indepyear desc;+-----------+| indepyear |+-----------+|      1994 ||      1993 ||      1992 ||      1991 ||      1990 |

查詢city表中的所有資料,如果表中的資料量巨大,一般不要這樣對資料進行查詢

select * from city;

從city表中查詢出所有資料,但是只顯示前3行

select * from city limit 3;+----+----------+-------------+----------+------------+| ID | Name     | CountryCode | District | Population |+----+----------+-------------+----------+------------+|  1 | Kabul    | AFG         | Kabol    |    1780000 ||  2 | Qandahar | AFG         | Qandahar |     237500 ||  3 | Herat    | AFG         | Herat    |     186800 |+----+----------+-------------+----------+------------+

從city表中查詢出資料,只顯示欄位name,district,countrycode資料,查詢匹配的條件為countrycode=‘AFG‘

select name,district,countrycode from city where countrycode=‘AFG‘;+----------------+----------+-------------+| name           | district | countrycode |+----------------+----------+-------------+| Kabul          | Kabol    | AFG         || Qandahar       | Qandahar | AFG         || Herat          | Herat    | AFG         || Mazar-e-Sharif | Balkh    | AFG         |+----------------+----------+-------------+

從country表中查詢出資料,只顯示字name,code2,indepyear資料,查詢匹配的條件為indepyear > ‘1992‘

select name,code2,indepyear from country where indepyear > ‘1992‘;+----------------+-------+-----------+| name           | code2 | indepyear |+----------------+-------+-----------+| Czech Republic | CZ    |      1993 || Eritrea        | ER    |      1993 || Palau          | PW    |      1994 || Slovakia       | SK    |      1993 |+----------------+-------+-----------+
分組與彙總GROUP 分組的目的往往是對分組後的資料進行"彙總操作"
avg()   返回指定列的平均值max()   返回指定列的最大值min()   返回指定列的最小值count() 返回指定列中非null值的個數sum()   返回指定列的所有值之和select count(Language) from countrylanguage group by CountryCode limit 10;+-----------------+| count(Language) |+-----------------+|               4 ||               5 ||               9 ||               1 ||               3 ||               4 |HAVING: 對分組彙總運算後的結果指定過濾條件select count(Language) from countrylanguage group by CountryCode having count(Language) > 5 limit 10;+-----------------+| count(Language) |+-----------------+|               9 ||               8 ||               8 ||               6 ||               7 ||               6 ||               7 ||               6 ||              12 ||              12 |+-----------------+查詢students表,以性別為分組,求出分組後的年齡之和。select gender,sum(age) from students group by gender;查詢students表,以classid分組,顯示平均年齡大於25的classid。select classid,avg(age) as avgage from students group by classid having avgage > 25;查詢students表,以性別欄位gender分組,顯示各組中年齡大於19的學員的年齡的總和。select sum(age) from students where age > 19 group by gender;

mysql(四)-增刪改查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.