MySQL基本查詢語句


1、查看當前使用的是哪個數據庫

mysql> select database();

另外,在下面2個語句的輸出里也能看出當前庫是哪一個

mysql> show tables;

mysql> status;

 

2、查看MySQL版本和狀態

mysql> select VERSION();

mysql> status;

 

3、查看MySQL實例的當前狀態(參數形式)

mysql> show status;

 

4、查看MySQL實例的參數

mysql> show variables;

查看最大連接數

mysql> show variables like '%max_connections%';

 

5、查看MySQL實例當前的進程

mysql> show processlist;

 

6、查詢所有數據

select * from Info 查所有數據

select Code,Name from Info 查特定列

 

7、根據條件查

select * from Info where Code='p001' 一個條件查詢 

select * from Info where Code='p001' and Natio n='n001' 多條件 並關系 查詢

select * from Info where Name='胡軍' or Nation='n001' 多條件 或關系 查詢

select * from Car where Price>=50 and Price<=60 范圍查詢

select * from Car where Price between 50 and 60 范圍查詢

 

8、模糊查詢

select * from Car where Name like '%型' %通配符代表任意多個字符

select * from Car where Name like '%奧迪%'

select * from Car where Name like '_馬%' _通配符代表任意一個字符

 

9、排序

select * from Car order by Price asc 按照價格升序排列

select * from Car order by Price desc 按照價格降序排列

select * from Car order by Price,Oil 按照兩列進行排序,前面的為主要的

 

10、統計函數(聚合函數)

select count(Code) from Car 查詢表中有多少條數據

select max(Price) from Car 取價格的最大值

select min(Price) from Car 取價格的最小值

select sum(Price) from Car 取價格的總和

select avg(Price) from Car 取價格的平均值

 

11、分組查詢

select Brand from Car group by Brand having count(*)>2 查詢所有系列中數量大於2的

 

12、分頁查詢

select * from Car limit 0,5 跳過幾條數據取幾條數據

 

13、去重查詢

select distinct Brand from Car

 

14、查詢建庫、建表語句

mysql> show create database dbname;

 

指定庫后才能查詢建表語句

mysql> show create table tablename;

 

15、查詢指定表的字段屬性

mysql> show full columns from tablename;

或者

mysql> show full fields from tablename;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM