
正文
我的PHP之旅--SQL语句
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
SQL语句
结构化查询语言(Structured Query Language)简称SQL,是一种操作数据的语言。
增加记录
-
INSERT INTO table_name(字段1, 字段2, 字段3)VALUES(值1, 值2, 值3);
- 字段与值是一一对应的。
- 增加记录只能一条一条的增加。
-
如果没有列出的字段将以默认值代替。
- 例:insert into news(title,content)values('一些标题','一些内容');
删除记录
- DELETE FROM table_name[WHERE 条件];
- delete from news; // 删除表内所有的数据。
- delete from news where id>10; // 删除id大于10的数据。
- delete from news where id>10 and id<35; //删除id大于10且小于35的数据。
- delete from news where author='某个作者' and id<100; // 删除author字段等于'某个作者'且id小于100的数据。
修改(更新)记录
- UPDATE table_name SET 字段1=新值1,字段2=新值2 [WHERE 条件];
- 例句:update news set title='新的标题',content='新的内容' where id=3;
查询记录
- SELECT 字段1,字段2 FROM table_name [WHERE 条件] [ORDER BY 排序] [LIMIT 限制输出(分页)];
- select * from news; // 把所有列所有数据都查询出来,当数据很多时不建议这样做。
- select id,title from news where id<100 or hits<50; // 把id小于100或点击率小于50的数据查询出来,只显示id和title列。
- select id,title from news where id<100 or hits<50 order by id DESC; // 按照id降序排列,如果是升序排列: order by id 就可以,或者后面加上ASC
- select id,title from news where id between 10 and 40 order by id limit 0,10; // 查询id在10到40之间的数据,以升序排列,从第0行起输出10条数据。






