sql常用命令整理


1.登錄 mysql -u root -p 
2.輸入 exit 或 quit 退出登錄
3.創建一個數據庫
create database 數據庫名 [其他選項];

例:create database samp_db character set gbk;

4.show databases; 命令查看已經創建了哪些數據庫
5、選擇所要操作的數據庫
一: 在登錄數據庫時指定 例如: mysql -D samp_db -u root -p
二: 在登錄后使用 use 語句指定, 命令: use 數據庫名;

常用命令:

1.select 某列 from table; 
2.select distinct 某列 from table; 列出不同(distinct)的值。
3.select 列名稱 from 表名稱 where 列 運算符 值;條件查詢(運算符:= <> >= 
between like4.select * from table where name='asdf'and id=1;and運算符實例
5.select * from table where (name='dasdf' or id=23) and a_id='23';or
+and混合運算符
6.select * from table order by 列名稱; order by 排序
7.insert into table values ('','' ,'','');插入新的行
8.insert into table (列名稱,列名稱) values ('','');給指定列插入數據
9.update table set 列名稱=新值,列名稱=新值 where id=2; Update修改表中的
數據。 
10.delete from table where id=1; 刪除某行
11.delete from table;刪除所有行
SQL 高級語法
12.select * from table limit 5;取5條
13.select top 2 * from table;取前兩條
14.select top 50 percent * from table;取50%數據
15.select * from table where 列名稱 like 'n%';"%" 可用於定義通配符(模式
中缺少的字母)。%:替代一個或多個字符。_:僅替代一個字符.
16.select * from table where id in (1,2,3,4);   IN 操作符
17.select * from table where id between   BETWEEN 操作符
18.select * from table_name as alias_name;  SQL Alias(別名)
19.select a.id,a.name,b.time from table1 as a left join table2 as b 
where a.id=b.uid oder by a,id desc;

下面列出了您可以使用的 JOIN 類型,以及它們之間的差異。
join(inner join): 如果表中有至少一個匹配,則返回行
left join: 即使右表中沒有匹配,也從左表返回所有的行
right join: 即使左表中沒有匹配,也從右表返回所有的行
full join : 只要其中一個表中存在匹配,就返回行

19.SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer;GROUP BY分組
20.select * from table where address is null; SQL 的 NULL 值查詢
21.select name a*(b + ifnull(c,0)) as num from table;

在 MySQL 中,我們可以使用 IFNULL() 函數驗證空值;

22.select avg(volumn_name) from table;

AVG 函數返回數值列的平均值。NULL 值不包括在計算中。

22.select count(column_name) from table;
COUNT(column_name) 函數返回指定列的值的數目(NULL 不計入)
23.select count(*) from table; null計入;
24.select first(column_name) from table;//第一條數據
25.select last(column_name) from table;//最后一條數據
26.select name format(time,'YYYY-MM-DD') as time from table;

 

FORMAT 函數用於對字段的顯示進行格式化。

 

 


免責聲明!

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



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