这篇是承接上一篇基础操作的文章的,在上一篇文章中主要介绍了以下内容:
(1)数据库基本操作
(2)数据表基本操作
(3)约束相关操作
(4)函数相关操作
因此这篇文章主要是从数据的增改删查角度来分析,其中查询是数据操作中最为麻烦的一种,但是这里给出一些常见的基本操作,关于其优化等等会在今后的文章中指出。感谢各位支持。
一、插入数据
1、向所有字段插入
![1c80fd231622b0ae565fd09a542e883a.png](https://img-blog.csdnimg.cn/img_convert/1c80fd231622b0ae565fd09a542e883a.png)
2、向指定字段插入
![8b5f206eefec5815f1f04f0c08f51a90.png](https://img-blog.csdnimg.cn/img_convert/8b5f206eefec5815f1f04f0c08f51a90.png)
二、修改数据
当然,我们也可以根据条件进行修改数据,比如使用where进行筛选即可,但这里只给出一个最简单的。
![41114c81f84f2160a25f312a47859757.png](https://img-blog.csdnimg.cn/img_convert/41114c81f84f2160a25f312a47859757.png)
三、删除数据
同样可以根据where进行有条件的删除。
![b8e27d33d9d9ed6ea800e70e776cb875.png](https://img-blog.csdnimg.cn/img_convert/b8e27d33d9d9ed6ea800e70e776cb875.png)
四、查询数据
这个查询数据是最麻烦的,我们在这里给出一些常见的查询。
1、查询所有字段和部分字段
![53865994402f3688022bbec0bbee628a.png](https://img-blog.csdnimg.cn/img_convert/53865994402f3688022bbec0bbee628a.png)
2、使用distinct去除重复数据
![8eb42d5582d46703112c80e54c202bf0.png](https://img-blog.csdnimg.cn/img_convert/8eb42d5582d46703112c80e54c202bf0.png)
3、使用limit指定查询结果的行数
![85238a377dbf0cfa05eae367bd036d06.png](https://img-blog.csdnimg.cn/img_convert/85238a377dbf0cfa05eae367bd036d06.png)
4、使用order by对结果进行排序
![81b303c5a202619f0f6d66a3ee92a71a.png](https://img-blog.csdnimg.cn/img_convert/81b303c5a202619f0f6d66a3ee92a71a.png)
5、使用where进行条件查询
(1)比较运算符
![c83ff51b5847380da09f32acc9804b33.png](https://img-blog.csdnimg.cn/img_convert/c83ff51b5847380da09f32acc9804b33.png)
(2)算术和逻辑运算符
![f87cce74d565b0d134cdec5ff731e79f.png](https://img-blog.csdnimg.cn/img_convert/f87cce74d565b0d134cdec5ff731e79f.png)
举俩例子,其他运算符用法类似:
![63999e11f5a455511f479ea22ee3c030.png](https://img-blog.csdnimg.cn/img_convert/63999e11f5a455511f479ea22ee3c030.png)
6、多表查询
先给出两张表:
![91f5f9742a93ee0c3b8f3ffe315bdc92.png](https://img-blog.csdnimg.cn/img_convert/91f5f9742a93ee0c3b8f3ffe315bdc92.png)
(1)内连接:只返回两个表都有,而且id相等的值
![2baf41c308d52b0481696daa2f1609e3.png](https://img-blog.csdnimg.cn/img_convert/2baf41c308d52b0481696daa2f1609e3.png)
(2)左连接:返回包括左表所有记录,还有和右表中相等的记录
![b194a7729d4684239b5af1a0d9d63071.png](https://img-blog.csdnimg.cn/img_convert/b194a7729d4684239b5af1a0d9d63071.png)
(3)右连接:返回包括右表所有记录,还有和左表中相等的记录
![0f880daf0a58361e781f29efdab72fc5.png](https://img-blog.csdnimg.cn/img_convert/0f880daf0a58361e781f29efdab72fc5.png)
7、子查询
也就是查询里面套查询
![6726c629edda46c641fa679501778213.png](https://img-blog.csdnimg.cn/img_convert/6726c629edda46c641fa679501778213.png)
这里面的用法太多了,而且也太麻烦了。因此给出了这个最简单的例子。
8、分组查询
(1)使用group by分组
![2a7d242546afced6a17e368164eb944c.png](https://img-blog.csdnimg.cn/img_convert/2a7d242546afced6a17e368164eb944c.png)
(2)使用having设置条件
![4979e12136673be24520af52b71e2547.png](https://img-blog.csdnimg.cn/img_convert/4979e12136673be24520af52b71e2547.png)
9、正则表达式
正则表达式是最灵活的一种,也是使用率比较高的一种。
![01f34dd4ed0f7f753dd08bc91583019b.png](https://img-blog.csdnimg.cn/img_convert/01f34dd4ed0f7f753dd08bc91583019b.png)
OK,到这里最基本的增删改查也整理了一遍,还有很多其他的基础知识点。今后接着整理。