MySQL是一款廣泛使用的關系型數據庫管理系統,常用于Web應用程序服務。
以下是MySQL中常用的SQL語句:
1.創建數據庫 CREATE DATABASE databasename; 2.選擇數據庫 USE databasename; 3.創建表格 CREATE TABLE tablename ( column1 datatype, column2 datatype, column3 datatype, .... ); 4.插入數據 INSERT INTO tablename (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); 5.更新數據 UPDATE tablename SET column1 = newvalue1, column2 = newvalue2, ... WHERE column = value; 6.刪除數據 DELETE FROM tablename WHERE column = value; 7.查詢數據 SELECT * FROM tablename; 8.按條件查詢 SELECT * FROM tablename WHERE column = value; 9.按多個條件查詢 SELECT * FROM tablename WHERE column1 = value1 AND column2 = value2; 10.按指定列排序 SELECT * FROM tablename ORDER BY column1, column2 DESC; 11.限制返回記錄條數 SELECT * FROM tablename LIMIT 5; 12.跨表連接查詢 SELECT orders.order_id, customers.customer_name FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id; 13.求平均值 SELECT AVG(column) FROM tablename; 14.求總數 SELECT COUNT(*) FROM tablename; 15.求最大值 SELECT MAX(column) FROM tablename; 16.求最小值 SELECT MIN(column) FROM tablename; 17.對查詢結果進行分組查詢 SELECT column, COUNT(*) FROM tablename GROUP BY column; 18.使用HAVING對分組結果再次篩選 SELECT column, COUNT(*) FROM tablename GROUP BY column HAVING COUNT(*) >1; 19.使用子查詢查詢 SELECT column1 FROM tablename WHERE column2 = (SELECT MAX(column2) FROM tablename); 20.使用正則表達式查詢 SELECT * FROM tablename WHERE column REGEXP '^[a-z]+$';