MySQL是一種關系型數據庫管理系統,是最流行的開源數據庫。
在MySQL中,查詢是最基本的操作之一。下面是一些MySQL查詢的實例:
-- 查詢表中所有的數據 SELECT * FROM table_name; -- 按條件查詢數據 SELECT column_name FROM table_name WHERE condition; -- 按條件查詢并按指定列排序 SELECT column_name FROM table_name WHERE condition ORDER BY column_name ASC/DESC; -- 查詢某列的不同值 SELECT DISTINCT column_name FROM table_name; -- 使用聚合函數計算數據 SELECT AVG(column_name) FROM table_name WHERE condition; SELECT COUNT(*) FROM table_name WHERE condition; SELECT MAX(column_name) FROM table_name WHERE condition; SELECT MIN(column_name) FROM table_name WHERE condition; SELECT SUM(column_name) FROM table_name WHERE condition; -- 多表查詢 SELECT column_name FROM table1_name, table2_name WHERE condition; -- 使用子查詢查詢數據 SELECT column_name FROM table_name WHERE column_name IN (SELECT column_name FROM another_table WHERE condition); -- 使用JOIN關鍵字連接多張表 SELECT column_name FROM table1_name JOIN table2_name ON condition; -- 對查詢結果進行分組 SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name; -- 使用HAVING關鍵字對分組數據進行過濾 SELECT column_name, COUNT(*), AVG(column_name) FROM table_name GROUP BY column_name HAVING COUNT(*) >1; -- 使用LIMIT關鍵字限制查詢結果數量 SELECT column_name FROM table_name LIMIT 20;