mysql基本表管理sql语句

管理表记录
insert into 表名 values(字段值列表) 增
select 字段名 …字段N from 表名; 查
select 字段名.. 字段N from表名 where 条件
update 表名 字段名=值 (where 条件) 改
delete from 表名 where 条件 删
逻辑匹配有and 有or时 先判断 and再判断or
()逻辑判断优先级
from user t2 left join t3
sql查询 表名 where 字名段名 in(范围)
sql查询 表名 where 字段名 not in(范围)
sql查询 表名 where between 数字1 and 数字2
select distinct from 表明 去掉字段重复显示查询结果

模糊查询
where 字段名 like —_匹配单个字符 %匹配0~N个字符
mysql> select name from usertab where name like “_“; 查询三个字符的名字
mysql> select name from usertab where name like “%b%”; 查询包含字母b的名字
mysql> select name from usertab where name like “%_%”; 查询至少有三个字符的名字
mysql> select id,name from usertab where name like “%”; null值不会显示出来

sql查询
sql查询 where 字段名 regexp 正则表达式 ^ .[]|mysql>selectnamefromusertabwhereoujielun|123||xietingfeng|123456nameregexp[09];mysql>selectnamefromusertabwherenameregexpa.d . [ ] ∗ | m y s q l > s e l e c t n a m e f r o m u s e r t a b w h e r e o u j i e l u n | 123 | | x i e t i n g f e n g | 123456 n a m e r e g e x p ‘ [ 0 − 9 ] ′ ; 名 字 里 包 含 数 字 的 m y s q l > s e l e c t n a m e f r o m u s e r t a b w h e r e n a m e r e g e x p ‘ a . ∗ d ’; 名字里以a开头d结尾的
mysql> select name from usertab where name regexp ‘^a|^d’; 名字里以a开头或者d开头的
mysql> select name,uid from usertab where uid regexp ‘….’; 至少匹配四位及以上的id
mysql> select name,uid from usertab where uid regexp ‘^….$’;只匹配四位的id
oujielun | 123 |
| xietingfeng | 123456

四则运算
select A+B A-B A*B A/B A%B from 表名
字段类型必须是数值类型
mysql> select uid,gid,uid+gid (as) sum from usertab where name=”bin”;
mysql> select name,uid,gid,(uid+gid)/3 “二分之一” from usertab where name=”bin”;
mysql> select name,uid,gid, uid+20 “增加id” from usertab where name=”bin”;

聚集函数
avg(字段名) 字段平均值
sum(字段名) 统计字段之和
min(字段名) 统计字段最小值
max(字段名) 统计字段最大值
count(字段名) 统计字段值个数

select 函数(字段名) from 表名 (条件);
count空值不算个数
函数不能作条件判断

mysql> select avg(pay) from usertab;
mysql> select max(id) from usertab;
mysql> select count(boy) from usertab;

查询结果排序
sql查询 order by 字段名 asc(升序从上到下) 或 desc(降序从上到下)
sql查询 group by 字段名 查询结果分组
限制查询结果的行数
sql查询 limt 数字; 前N条
sql查询 limit 数字1,数字2 范围内,数字1行开始(从0开始),显示数字2行
mysql> select id,name from usertab where id<=20 limit 5;
mysql> select id,name from usertab where id<=20 limit 2,3;

sql查询 having 条件表达式
sql查询 where 条件 having 条件表达式
mysql> select uid,name from usertab where uid<30 having name=”bin”;
mysql> select uid,name from usertab where uid<30 and name=”bin”;
两种查询结果一样 执行过程不一样
having后的字段必须前面有出现的 否则报错

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

复制表 ( 作用备份表,快速建表)
create table 表名1 select * from 表名2
create table 表名 sql查询语句
crteate table 表名 sql查询 where 不成立条件 建立同种结构空表
create table 表1 select * from 表2 where 不成立的条件 (这种方法会丢失键值)
或者 create table 表1 like 表2 只复制表结构不复制数据

多表查询
select 字段名列表 from 表a,表b
笛卡尔集
select * from t1,t2
select t1.(字段),t2(字段) from t1,t2
显示 t1行数*t2行数

嵌套查询:把内存的查uxn结果作为外层查询的查询条件索引类型
selcet 字段名列表 from 表名 where 条件(select 字段名列表 from 表名 where 条件)
mysql> select name,uid from db3.usertab where uid<( select avg(uid) from db3.usertab );
mysql> select name from db3.usertab where name in (select name from db4.t2);查t2表有哪些名字在usertab表存在
连接查询
左连接查询:以左边的表为主显示查询结果
select字段名列表 from 表名 left join 表名 on 条件
mysql> select * from t3 left join t4 on t3.uid = t4.uid;
右连接查询:以右边的表为主显示查询结果
select 字段名列表 from 表名A right join 表名B on 条件
mysql> select * from t3 right join t4 on t3.uid = t4.uid;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值