SQL基本入门

SQL基本入门

一.数据库查询语句:select

  1. 查询所有数据:
    select * from 表名;
    select * from exam_books;

2.按照一定的条件查找:
select * from 表名 where 条件;
select * from exam_books where id<20;

3.范围条件查询:
select * from 表名 where 字段 between 值1 and 值2 ;
select * from exam_books where id<20 and id>10;
select * from exam_books where id between 10 and 20;
select * from exam_books where addtime between ‘2011-03-17 00:00:00’ and ‘2011-03-18 00:00:00’;

4.模糊查询:
select * from 表名 where 字段 like ‘%条件%’;
select * from exam_books where bookname like ‘%马克思%’;

5.复合查询:
select * from 表名 where 条件 and 另一个条件;
select * from exam_questions where coursecode = ‘03706’ and chapterid = 2;

6.查询个数:
select count(*) from 表名 where 条件
select count(*) as 别名 from exam_questions where coursecode = ‘03706’ and chapterid = 2;

7.查询结果按顺序排列:
正序、倒序(asc/desc)
select * from 表名 where 条件 order by 字段 desc/asc;
select * from exam_questions where coursecode = ‘03706’ order by chapterid;

8、按照limit查找某个范围内的数据:
SELECT * FROM exam_questions order by id asc limit 0, 15;(代表查找出的第一页的信息)
SELECT * FROM exam_questions order by id asc limit 15, 15;(代表查找出的第二页的信息)
【备注:】limit后面的两个数字中:第一个代表偏移量,第二个代表每页展示的数量

二. 删除数据:delete
delete from 表名 where 条件;
delete from exam_questions where id<2000;
delete from exam_questions where coursecode=’00041’ and chapterid=1;

三.插入新数据:insert
insert into 表名(字段) values(值);
insert into exam_weburl(webname , weburl , info , bigtypeid) values(‘人人网’, ‘renren.com’ , ‘这个网站不错’ , 3);

四.更新数据:update
update 表名 set 字段1=值1, 字段2=值2 where 条件;
update exam_weburl set info=’这个网站不太好’ where id=73;
update exam_weburl set webname=’人人2’, weburl=’www.renren.com’ where id=73;

五、更新表结构的语句:
1、如需在表中添加列,请使用下列语法:
ALTER TABLE table_name
ADD column_name datatype
2、要删除表中的列,请使用下列语法:
ALTER TABLE table_name
DROP COLUMN column_name

3、要改变表中列的数据类型,请使用下列语法:
ALTER TABLE table_name
ALTER COLUMN column_name datatype

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值