db连接mysql这么写_sql连接数据库语句怎么写?sql连接数据库语句范例解析

常用数据库sql语句写法整理

--创建一个用户表

create table users (id int(10),pwd char(20),name varchar(10));

char表示占20个长度的空间 varchar根据实际长度决定空间大小

--复制表

create table users_copy as select * from users;

--增加数据到表中

方式1:insert into users values(1,‘1234’,‘d1’);

方式2:insert into users (id,name)values(2,‘d2’);

--查询数据

最简单的 select * from users;

条件查询 select * from users where id=1 ;

select * from users where id=1 and name=‘d1’ ;(and表示且的意思)

select * from users where id=1 or id=2 ;(or表示或的意思)

select * from users where id in(1,3,4);(or的条件多了,用in)

select * from users where id》=2;(可以有大小条件)

select count(*) from users;(查询有多少条数)

select * from employ where name like ‘%duan%’;(模糊查询)

select * from employ where name like ‘duan%’;(模糊查询,表示以duan开头---结尾反之)

select distinct name from users;(除去重复记录)

select * from employ order by id;(由小到大)

select * from employ order by id desc;(反之顺序排列)

查询的时候可以计算

select name,salary salary*12 from employ;(员工的年薪)

select name,salary+bonus from employ;(员工的月薪)

分组查询

select job, count(*)from emp_ning group by job;

过滤分组结果:having短语

select deptno, count(*)from emp_ning group by deptno having count(*) 》= 5;

子查询(即用某一查询语句代替语句中要写的数据)

select name from emp_ning where salary 》 (select salary

from emp_ning where id = 1002);

--子查询的结果集是多行多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值