您好,登錄后才能下訂單哦!
下文主要給大家?guī)?a title="MySQL" target="_blank" href="http://kemok4.com/mysql/">MySQL該如何操作數(shù)據(jù)表中的記錄,希望這些內(nèi)容能夠帶給大家實際用處,這也是我編輯MySQL該如何操作數(shù)據(jù)表中的記錄這篇文章的主要目的。好了,廢話不多說,大家直接看下文吧。
1.插入記錄 INSERT
INSERT [INTO] tbl_name [(col_name,...)] {VAULES|VALUE} ({expr|DEFAULT},...),(...),...
例,插入單條記錄:
mysql> USE testDatabase changed mysql> CREATE TABLE users( -> id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, -> username VARCHAR(20) NOT NULL, -> password VARCHAR(32) NOT NULL, -> age TINYINT UNSIGNED NOT NULL DEFAULT 10, -> sex BOOLEAN -> ); mysql> INSERT users VALUES(NULL,'Tom','asd123',24,1); mysql> SELECT * FROM users;+----+----------+----------+-----+------+| id | username | password | age | sex |+----+----------+----------+-----+------+| 1 | Tom | asd123 | 24 | 1 |+----+----------+----------+-----+------+
插入多條記錄:
mysql> INSERT users VALUES(DEFAULT,'John','asd123',24,1), -> (NULL,'Huang','daddd',25,1); mysql> SELECT * FROM users; +----+----------+----------+-----+------+| id | username | password | age | sex |+----+----------+----------+-----+------+| 1 | Tom | asd123 | 24 | 1 || 2 | John | asd123 | 24 | 1 || 3 | Huang | daddd | 25 | 1 |+----+----------+----------+-----+------+
插入數(shù)學(xué)表達(dá)式:
mysql> INSERT users VALUES(NULL,'John','asd123',3*7-5,1); mysql> SELECT * FROM users;+----+----------+----------+-----+------+| id | username | password | age | sex |+----+----------+----------+-----+------+| 1 | Tom | asd123 | 24 | 1 || 2 | John | asd123 | 24 | 1 || 3 | Huang | daddd | 25 | 1 || 4 | John | asd123 | 16 | 1 |+----+----------+----------+-----+------+
插入md5的哈希密碼:
mysql> INSERT users VALUES(NULL,'John',md5('123'),DEFAULT,0); mysql> SELECT * FROM users;+----+----------+----------------------------------+-----+------+| id | username | password | age | sex |+----+----------+----------------------------------+-----+------+| 1 | Tom | asd123 | 24 | 1 || 2 | John | asd123 | 24 | 1 || 3 | Huang | daddd | 25 | 1 || 4 | John | asd123 | 16 | 1 || 5 | John | 202cb962ac59075b964b07152d234b70 | 10 | 0 |+----+----------+----------------------------------+-----+------+
2.插入記錄 INSERT SET-SELECT
(與第一種方式的區(qū)別是 可以使用子查詢,不可以同時插入多條記錄)
INSERT [INTO] tbl_name SET col_name={expr|DEFAULT},...
例:
mysql> INSERT users SET username='Ben',password=md5('345'); mysql> SELECT * FROM users;+----+----------------------+----------------------------------+-----+------+| id | username | password | age | sex |+----+----------------------+----------------------------------+-----+------+| 1 | Tom | asd123 | 24 | 1 || 2 | John | asd123 | 24 | 1 || 3 | Huang | daddd | 25 | 1 || 4 | John | asd123 | 16 | 1 || 5 | John | 202cb962ac59075b964b07152d234b70 | 10 | 0 || 6 | Ben | d81f9c1be2e08964bf9f24b15f0e4900 | 10 | NULL |+----+----------------------+----------------------------------+-----+------+
3.更新記錄 UPDATE
單表更新
UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1={expr1|DEFAULT} [,col_name2={expr2|DEFAULT}]... [WHERE where_condition]
例,將所有記錄的年齡加5:
mysql> UPDATE users SET age = age+5; mysql> SELECT * FROM users; +----+----------------------+----------------------------------+-----+------+| id | username | password | age | sex |+----+----------------------+----------------------------------+-----+------+| 1 | Tom | asd123 | 29 | 1 || 2 | John | asd123 | 29 | 1 || 3 | Huang | daddd | 30 | 1 || 4 | John | asd123 | 21 | 1 || 5 | John | 202cb962ac59075b964b07152d234b70 | 15 | 0 || 6 | Ben | d81f9c1be2e08964bf9f24b15f0e4900 | 15 | NULL |+----+----------------------+----------------------------------+-----+------+
將Tom的年齡加10歲:
mysql> UPDATE users SET age = age+10 WHERE id=1; mysql> SELECT * FROM users WHERE id=1;+----+----------+----------+-----+------+| id | username | password | age | sex |+----+----------+----------+-----+------+| 1 | Tom | asd123 | 39 | 1 |+----+----------+----------+-----+------+
將id為偶數(shù)的年齡加10歲:
mysql> UPDATE users SET age = age+10 WHERE id % 2 = 0; mysql> select * from users; +----+----------------------+----------------------------------+-----+------+| id | username | password | age | sex |+----+----------------------+----------------------------------+-----+------+| 1 | Tom | asd123 | 39 | 1 || 2 | John | asd123 | 39 | 1 || 3 | Huang | daddd | 30 | 1 || 4 | John | asd123 | 31 | 1 || 5 | John | 202cb962ac59075b964b07152d234b70 | 15 | 0 |+----+----------------------+----------------------------------+-----+------+
修改所有記錄的sex為0:
mysql> UPDATE users SET sex = 0; mysql> select * from users; +----+----------------------+----------------------------------+-----+------+| id | username | password | age | sex |+----+----------------------+----------------------------------+-----+------+| 1 | Tom | asd123 | 39 | 0 || 2 | John | asd123 | 39 | 0 || 3 | Huang | daddd | 30 | 0 || 4 | John | asd123 | 31 | 0 || 5 | John | 202cb962ac59075b964b07152d234b70 | 15 | 0 || 7 | Ben | d81f9c1be2e08964bf9f24b15f0e4900 | 15 | 0 |+----+----------------------+----------------------------------+-----+------+
4.刪除記錄 DELETE
單表刪除 (若不添加WHERE則刪除【全部記錄】)
DELETE FROM tbl_name [WHERE where_condition]
例,刪除id=7的記錄:
mysql> DELETE FROM users WHERE id = 7; mysql> select * from users; +----+----------+----------------------------------+-----+------+| id | username | password | age | sex |+----+----------+----------------------------------+-----+------+| 1 | Tom | asd123 | 39 | 0 || 2 | John | asd123 | 39 | 0 || 3 | Huang | daddd | 30 | 0 || 4 | John | asd123 | 31 | 0 || 5 | John | 202cb962ac59075b964b07152d234b70 | 15 | 0 |+----+----------+----------------------------------+-----+------+
5.查找記錄 SELECT
SELECT select_expr [,select_expr...][ FROM tbl_references [WHERE where_condition] [GROUP BY {col_name | position} [ASC | DESC],...] [HAVING where_condition] [ORDER BY {col_name | expo | position} [ASC | DESC],...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] ]
查詢表達(dá)式(select_expr)
1. 每一個表達(dá)式表示想要的一列,必須有至少一個
2. 多個列之間以英文逗號分隔
3. 星號(*)表示所有列;tbl_name.*可以表示命名表的所有列
4. 查詢表達(dá)式可以使用[AS]alias_name為其賦予別名
5. 別名可用于GROUP BY,ORDER BY或HAVING子句
SELECT查詢表達(dá)式字段出現(xiàn)的順序?qū)⒂绊懡Y(jié)果集字段出現(xiàn)的順序
字段的別名也會影響以后的結(jié)果集 , 在以后的PHP中有重要作用
例,只查詢users表中 id 和 username 兩列:
mysql> SELECT id,username FROM users;+----+----------+| id | username |+----+----------+| 1 | Tom || 2 | John || 3 | Huang || 4 | John || 5 | John |+----+----------+
查詢users表中 id 和 username 兩列,分別用userid,uname作為別名:
mysql> SELECT id AS userid ,username AS uname FROM users; +--------+-------+| userid | uname |+--------+-------+| 1 | Tom || 2 | John || 3 | Huang || 4 | John || 5 | John |+--------+-------+
6.where語句進(jìn)行條件查詢
條件表達(dá)式(WHERE)
對記錄進(jìn)行過濾,如果沒有指定WHERE子句,則顯示所有記錄
在WHERE表達(dá)式中,可以使用MySQL支持的函數(shù)或運算符
7.group by語句對查詢結(jié)果分組
group by
查詢結(jié)果分組
[GROUP BY {col_name | position} [ASC | DESC],...]
可以制定列的列名和列的位置。
asc生序,desc降序,多個分組中間用逗號區(qū)分。
例,查詢users表,按照性別分組:
mysql> SELECT sex FROM users GROUP BY sex;+------+| sex |+------+| 0 |+------+
8.having語句設(shè)置分組條件
分組條件(HAVING)
[HAVING where_condition]
其中,where_condition要么是聚合函數(shù)(max,min,avg,count,sum),要么其中的字段必須是SELECT中的一個查詢字段,否則會報錯]
例,查詢users表中sex分組,條件為age>20(這種方法需要查詢字段也添加age字段):
mysql> SELECT sex,age FROM users GROUP BY sex having age > 20; +------+-----+| sex | age |+------+-----+| 0 | 39 |+------+-----+
查詢users表中sex分組,條件為id >=2 :
mysql> SELECT sex FROM users GROUP BY sex HAVING count(id) >= 2; +------+| sex |+------+| 0 |+------+
9.order by語句對查詢結(jié)果排序
對查詢結(jié)果進(jìn)行排序(ORDER BY)
[ORDER BY {col_name | expr | position} [ASC | DESC],...]
可以按照多個列排序
例,查詢users表,按年齡從小到大排列:
mysql> SELECT * FROM users ORDER BY age ASC; +----+----------+----------------------------------+-----+------+| id | username | password | age | sex |+----+----------+----------------------------------+-----+------+| 8 | Hui | 9e1e06ec8e02f0a0074f2fcc6b26303b | 8 | 1 || 5 | John | 202cb962ac59075b964b07152d234b70 | 15 | 0 || 10 | Luyuan | 9e1e06ec8e02f0a0074f2fcc6b26303b | 21 | 1 || 9 | Dui | 9e1e06ec8e02f0a0074f2fcc6b26303b | 27 | 1 || 3 | Huang | daddd | 30 | 0 || 12 | Murouan | 9e1e06ec8e02f0a0074f2fcc6b26303b | 31 | 1 || 11 | Oduyuan | 9e1e06ec8e02f0a0074f2fcc6b26303b | 31 | 1 || 13 | Spqoan | 9e1e06ec8e02f0a0074f2fcc6b26303b | 31 | 1 || 4 | John | asd123 | 31 | 0 || 2 | John | asd123 | 39 | 0 || 1 | Tom | asd123 | 39 | 0 |+----+----------+----------------------------------+-----+------+
查詢users表,第一順序按年齡從小到大排列,第二順序按id從小到大排列:
mysql> SELECT * FROM users ORDER BY age,id ASC;+----+----------+----------------------------------+-----+------+| id | username | password | age | sex |+----+----------+----------------------------------+-----+------+| 8 | Hui | 9e1e06ec8e02f0a0074f2fcc6b26303b | 8 | 1 || 5 | John | 202cb962ac59075b964b07152d234b70 | 15 | 0 || 10 | Luyuan | 9e1e06ec8e02f0a0074f2fcc6b26303b | 21 | 1 || 9 | Dui | 9e1e06ec8e02f0a0074f2fcc6b26303b | 27 | 1 || 3 | Huang | daddd | 30 | 0 || 4 | John | asd123 | 31 | 0 || 11 | Oduyuan | 9e1e06ec8e02f0a0074f2fcc6b26303b | 31 | 1 || 12 | Murouan | 9e1e06ec8e02f0a0074f2fcc6b26303b | 31 | 1 || 13 | Spqoan | 9e1e06ec8e02f0a0074f2fcc6b26303b | 31 | 1 || 1 | Tom | asd123 | 39 | 0 || 2 | John | asd123 | 39 | 0 |+----+----------+----------------------------------+-----+------+
10.LIMIT語句限制查詢數(shù)量
LIMIT 限制查詢結(jié)果返回的數(shù)量
[LIMIT{[offset,]row_count|row_count OFFSET offset}]
SELECT * from users #返回所有的結(jié)果 SELECT * from users limit 2 #返回所有的結(jié)果集中前兩條
mysql> SELECT * FROM users LIMIT 2;+----+----------+----------+-----+------+| id | username | password | age | sex |+----+----------+----------+-----+------+| 1 | Tom | asd123 | 39 | 0 || 2 | John | asd123 | 39 | 0 |+----+----------+----------+-----+------+
SELECT * FROM users LIMIT 2,2; #要查詢結(jié)果集中第3條和第4條記錄。mysql中的記錄是從0開始的,共幾條。
mysql> SELECT * FROM users LIMIT 2,2;+----+----------+----------+-----+------+| id | username | password | age | sex |+----+----------+----------+-----+------+| 3 | Huang | daddd | 30 | 0 || 4 | John | asd123 | 31 | 0 |+----+----------+----------+-----+------+
總結(jié)
記錄操作: INSERT , UPDATE , DELETE , SELECT
1.insert的三種
insert [into] 表名 [(列名,列名)]{values|value} ({(表達(dá)式|default},())insert [into] 表名 set 列名={(表達(dá)式|default},。。insert [into] 表名 [(列名,列名)] SELECT..
2.UPDATE
(1)單表更新
UPDATE [LOW_PRIORITY][IGNORE] table_reference SET col_name1={expr1|DEFAULT},[col_name2={expr2|DEFAULT}]...[WHERE where_condition]
多表更新
3.DELETE : 刪除數(shù)據(jù)
(1)單表刪除
DELETE FROM tbl_name [WHERE where_condition}
4.SELECT: //查詢
SELECT select_expr [,select expr2...] #只查找某一個函數(shù)或表達(dá)式[FROM table_references #查詢表名[WHERE where_conditon] #查詢條件[GROUP BY {col_name|position} [ASC|DESC],...] #按某個字段進(jìn)行分組,相同的只顯示第一個[HAVING where_conditon] #分組時,給出顯示條件[ORDER BY {col_name|expr|position} [ASC|DESC],...] #排序[LIMIT {[offset,]row_count|row_count OFFSET offset}] #限制返回數(shù)量]
SELECT
FROM 表的參照
WHERE 條件
GROUP BY 對記錄結(jié)果進(jìn)行分組
HAVING 對分組條件的設(shè)定
結(jié)合內(nèi)容,將查找的結(jié)果存儲在指定的數(shù)據(jù)表:
mysql> CREATE TABLE test( -> id TINYINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> username VARCHAR(20) NOT NULL -> );
mysql> INSERT test(username) SELECT username FROM users WHERE age >= 25; mysql> SELECT * FROM test; +----+----------+| id | username |+----+----------+| 1 | Tom || 2 | John || 3 | Huang || 4 | John || 5 | Dui || 6 | Oduyuan || 7 | Murouan || 8 | Spqoan |+----+----------+
對于以上關(guān)于MySQL該如何操作數(shù)據(jù)表中的記錄,大家是不是覺得非常有幫助。如果需要了解更多內(nèi)容,請繼續(xù)關(guān)注我們的行業(yè)資訊,相信你會喜歡上這些內(nèi)容的。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。