学习-sql篇

前言:

这是sql的学习日志,将持续更新?,并且希望自己能学习到严谨精神。

1. 如何连接数据库
格式: mysql -h主机地址 -u用户名 -p用户密码
连接远程机器: mysql -h10.4.3.188 -uptsdb -p
等价写法 mysql --host=10.4.3.188 --user=ptsdb --password
连接本地机器: mysql -uroot -p
等价写法 mysql --user=root -p
(注:u与root可以不用加空格,其它也一样)
下面对以上参数进行解释说明。
-u:用户名。
-h:MySQL所在服务器地址。
-p:用户密码。 输入完命令语句后,按下〈Enter〉键就进入到了数据库中。

$ mysql -h 地址 -u 用户名 -p 密码 dbname

2.使用数据库

#列出所有db

show databases;;

#使用db1

use db1;

#列出db1中所有表

show tables;

#展示出table1表结构

desc table1;

3.表查询
#查询表中前两行数据

select * from table1 limit 2;
select * from table1 limit 2 \G;  #竖着打印出来

#查询表中前两行数据的字段1、字段2

select 字段1,字段2 from table1 limit 2 \G;

3.表查询-where
#在table1表中查询字段值等于1的数据

select * from table1 where 字段=1;

注:常见运算符

= 等于   != 不等于    >大于   < 小于    <= 小于等于 ......

#where 区间查询 between a and b; 闭区间[a,b]

select 字段1,字段2 from table1 where 字段 between a and b;

#where 查询 in (值1、值2、值3、、);只要有个一个等于就返回,且有先后顺序,先查询值1,其次值2这样子。

select ids,types,content_types from rec_contents where id in (142714590,143105504,...);

#where查询字符串

select 字段1,字段2 from rec_content where 字段名儿=‘isxue';

#where 模糊查询

select 字段1,字段2 from rec_content where 字段名儿 like ‘is';

注:
‘%’通配符,代表0-n 个 任意字符
‘-’代表任意字符

select 字段1,字段2 from rec_content where 字段名儿 like ‘is%';
select 字段1,字段2 from rec_content where 字段名儿 like ‘_is%';#is前边有一位字符

#where is

select 字段1,字段2 from rec_content where 字段3 is null;

#where 与条件连接符(and逻辑与,or逻辑或,not非)

and    select id, first_name ,salary from s_emp where salary>=1450 and salary<=250;
or    select id, first_name, dept_id from s_emp where dept_id=31 or dept_id=32 or dept_id=50;
not    select id, first_name, manager_id from s_emp where manager_id is not null

#union 表联合查询
注:针对两个集合进行操作的,连个集合必须有相同的列数,列具有相同的数据类型,且查询数据的列名是有第一个集合的列名来确定的,可以as一下,

union #连接查询#两个集合#两个集合必须有相同的列数,列具有相同的数据类型
SELECT 列名称 FROM 表名称 UNION SELECT 列名称 FROM 表名称 ORDER BY 列名称;
union all #连接查询#不带去重#两个集合#两个集合必须有相同的列数,列具有相同的数据类型
SELECT 列名称 FROM 表名称 UNION ALL SELECT 列名称 FROM 表名称 ORDER BY 列名称;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值