1. 程式人生 > >MySQL增刪改查操作例項

MySQL增刪改查操作例項

學習MySQL中對於表這種MySQL物件的增刪改查操作後,列出一些例項,在此用於記錄對應功能的語法細節,便於日後遺忘時檢視。

需求:插入一條學生資訊
1.插入一條資料
INSERT INTO t_student (name) VALUES('陸小鳳')
2.插入多條資料
INSERT INTO  t_student(name,email,age) VALUES('A','[email protected]',18),('B','[email protected]',52)
4.插入查詢結果(測試)
INSERT INTO t_student(name) SELECT name FROM t_student

需求:修改陸小鳳的名字為西門吹雪
UPDATE t_student SET name = '西門吹雪' WHERE NAME = '陸小鳳'
例子
UPDATE t_student SET name = 'LHLJ',age = 15, email ='
[email protected]
' WHERE id = 3 需求:刪除id為3的學生資訊 DELETE FROM t_student WHERE id=3 查詢所有資訊 SELECT * FROM t_student 查詢名字 SELECT id FROM t_student 去重查詢 SELECT DISTINCT dir_id from product 查詢貨品id,名稱和批發價(賣價*折扣) SELECT id,producrNmae,cutoff*saleprice FROM product 設定別名 SELECT id AS pid FROM product 需求:不要輸出表,要輸出“xxx商品的零售價為xxx” SELECT CONCAT(productname,'商品的零售價為',saleprice) FROM product 需求:條件查詢:查詢所有貨品名為'羅技' SELECT * FROM product WHERE productName = '羅技' 需求:查詢分類號為2或3的商品 SELECT * FROM product where dir_id = 2 OR dir_id = 3 SELECT * FROM product where dir_id IN (2,4) 範圍查詢:零售價300-400的貨品 SELECT * FROM product where saleprice BETWEEN 300 AND 400 IS NULL 用來判斷列的值是否為空 SELECT * FROM product where saleprice IS NULL 模糊查詢:如搜尋“李“ 關鍵字:LIKE LIKE 的萬用字元: _:必須佔據一個位置,任意字元 %:可以佔據0~n個位置,任意字元 比如: WHERE name LIKE '張_三' 搜尋張某三 WHERE name LIKE '張%' 搜尋張某某 WHERE name LIKE '%張%' 搜尋名字中帶“張”