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 函数用于对字段的显示进行格式化。

 

 

猜你喜欢

转载自www.cnblogs.com/mverting/p/9066030.html