符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
本文主要给大家介绍SELECT单表、多表查询升级及插入删除,其所涉及的东西,从理论知识来获悉,有很多书籍、文献可供大家参考,从现实意义来讲,创新互联累计多年的实践经验可分享给大家。
成都网站制作、成都做网站,成都做网站公司-成都创新互联已向上1000家企业提供了,网站设计,网站制作,网络营销等服务!设计与技术结合,多年网站推广经验,合理的价格为您打造企业品质网站。
SELECT:查询
SELECT select-list FROM tb WHERE qualification 根据标准qualification查找对应的行
查询语句类型: qualification条件 field领域 distinct独特的,没有重复的
简单查询:
多表查询:
子查询:
SELECT * FROM tb_name: 查询tb_name表的所有信息
SELECT field1,field2 FROM tb_name: 投影显示所设定的领域条目(field),一个field就是一列
SELECT [DISTINCT] * FROM tb_name WHERE qualification;从tb_name表中选择符合条件的独特的不重复的条目。
FROM子句:表、多个表、其他SELECT语句
WHERE子句:布尔关系表达式 =、>、<、<=、>= 表示大于等于,小于等于,大于,小于,等于。
逻辑关系:AND(与&&) OR(或||) NOT(非!) XOR(异或)
MySQL>SELECT Name,Age,Gender FROM students WHERE NOT Age>20 AND NOT Gender='M'; #选择查询年龄不大于20,且性别不是男的数据,也可以写成下面的条件
mysql> SELECT Name,Age,Gender FROM students WHERE NOT ( Age>20 OR Gender='M' );
特殊关系:BETWEEN ... AND ... 在两者之间之间。
LIKE ''
%:任意长度任意字符
_:任意单个字符
Usage: SELECT Name FROM students WHERE Name LIKE 'Y%'; 查找Name字段以Y开头的数据。
SELECT Name FROM students WHERE Name LIKE '%ing%';查找Name字段中必须包括ing的字段
SELECT Name FROM students WHERE Name LIKE 'Y___';查找Name字段中Y后面至少跟着3个字符的
REGEXP或者RLIKE 支持正则表达式:
Usage:SELECT Name,Age FROM students WHERE Name RLIKE '^[XY].*$';
查找Name字段行首为X或Y的数据
IN离散条件的查找:
Usage: SELECT Name,Age FROM students WHERE Age IN (18,20,25);
查找Age字段为18或20或25岁的相关数据
有空值的时候,比较:IS NULL ,NOT NULL ORDER BY ... 以...某字段升序排列
ORDER BY ... 以...某字段降序排列
Usage: SELECT Name,Age FROM students WHERE Name IS NULL;
查找Name字段为空值的数据
SELECT Name,Age FROM students WHERE Name IS NULL;
查找Name字段不为空值的数据
按照升序或者降序排列出查找的数据:ORDER BY field_name {ASC|DESC};
ASC升序排列(默认值)DESC降序排列
SELECT Name,Age FROM students WHERE CID IS NOT NULL ORDER BY Name;
查找CID字段不为空的数据并按字段Name的升序排列;
SELECT Name,Age FROM students WHERE CID IS NOT NULL ORDER BY Name DESC;
查找CID字段不为空的数据并按字段Name的降序排列;
字段别名:AS
Usage: SELECT Name AS Student_Name FROM student;
mysql> SELECT Name FROM student;
+------------+
| Name |
+------------+
| Li Lianjie |
| Cheng Long |
| Yang Guo |
| Guo Jing |
+------------+
4 rows in set (0.00 sec)
mysql> SELECT Name AS Student_Name FROM student;
+--------------+
| Student_Name| AS别名为Student_Name
+--------------+
| Li Lianjie |
| Cheng Long |
| Yang Guo |
| Guo Jing |
+--------------+
4 rows in set (0.00 sec)
LIMIT子句:LIMIT [offset,]Count offset偏移多少,Count显示多少
Usage: SELECT Name AS Student_Name FROM student LIMIT 3;
查找显示Name别名为Student_Name,只显示前3个数据
Usage: SELECT Name AS Student_Name FROM student LIMIT 2,2;
查找显示Name别名为Student_Name,偏移掉前2个数据不显示,显示第2个数据后的2个数据
mysql> SELECT Name AS Student_Name FROM student LIMIT 3;
+--------------+
| Student_Name |
+--------------+
| Li Lianjie |
| Cheng Long |
| Yang Guo |
+--------------+
3 rows in set (0.00 sec)
mysql> SELECT Name AS Student_Name FROM student LIMIT 2,2;
+--------------+
| Student_Name |
+--------------+
| Yang Guo |
| Guo Jing |
+--------------+
2 rows in set (0.00 sec)
聚合运算:SUM()求和,MIN()最小值,MAX()最大值,AVG()平均值,COUNT()统计字段中相同数值的个数;
新建立一张表:
mysql> CREATE TABLE class(ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,Name CHAR(20) NOT NULL UNSIGNED, Age TINYINT NOT NULL);
mysql> INSERT INTO class (Name,Age) VALUES ('Yang Guo',22),('Guo Jing',46),('Xiao Longnv',18),('Huang Rong',40);
mysql> DESC class;
+-------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(20) | NO | | NULL | |
| Age | tinyint(4) | NO | | NULL | |
+-------+------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM class;
+----+-------------+-----+
| ID | Name | Age |
+----+-------------+-----+
| 1 | Yang Guo | 22 |
| 2 | Guo Jing | 46 |
| 3 | Xiao Longnv | 18 |
| 4 | Huang Rong | 40 |
+----+-------------+-----+
4 rows in set (0.00 sec)
mysql> SELECT SUM(Age) FROM class;
+----------+
| SUM(Age) |
+----------+
| 126 |
+----------+
1 row in set (0.02 sec)
mysql> SELECT MAX(Age) FROM class;
+----------+
| MAX(Age) |
+----------+
| 46 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT MIN(Age) FROM class;
+----------+
| MIN(Age) |
+----------+
| 18 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT AVG(Age) FROM class;
+----------+
| AVG(Age) |
+----------+
| 31.5000 |
+----------+
1 row in set (0.00 sec)
分组:GROUP BY ... HAVING qualification 根据...分组 并且满足条件qualification
对于GROUP BY的条件选择需用HAVING作为条件筛选,而不是用WHERE
mysql> SELECT Age,Gender FROM class1 GROUP BY Gender; #以性别分组显示
+-----+--------+
| Age | Gender |
+-----+--------+
| 18 | F |
| 22 | M |
+-----+--------+
2 rows in set (0.00 sec)
mysql> SELECT AVG(Age),Gender FROM class1 GROUP BY Gender; #求男同学和女同学的平均年龄。
+----------+--------+
| AVG(Age) | Gender |
+----------+--------+
| 29.0000 | F |
| 34.0000 | M |
+----------+--------+
2 rows in set (0.00 sec)
mysql> SELECT Name,AVG(Age) FROM class1 GROUP BY Gender HAVING Name RLIKE 'Y.*';
#求以Gender分组的年龄平局值,且只显示以Y开头的Name字段的平均值和姓名
+----------+----------+
| Name | AVG(Age) |
+----------+----------+
| Yang Guo | 34.0000 |
+----------+----------+
1 row in set (0.00 sec)
SELECT用法归纳:
多表查询:
连接:
交叉连接:笛卡尔乘积 (Usag:SELECT * FROM students,course;查询students和course表的 内容)
自然连接:
Usage: SELECT students.Name,courses.Course FROM students,courses WHERE students.CID1=courses.CID 查询students表和courses表CID1=CID的选项,并显示名称
外连接:
左外连接:左表 LEFT JOIN 右表 ON 条件
右外连接:左表 RIGHT JOIN 右表 ON 条件
Usage:
SELECT s.Name,c.Name FROM students AS s LEFT JOIN courses AS c ON s.CID1=c.CID;
显示左表中所有学生,并查看他们所选修的课程名称,没有选修课程的直接显示NULL
SELECT s.Name,c.Name FROM students AS s RIGHT JOIN courses AS c ON s.CID1=c.CID;
查看右表中所有选修课程,并查看有哪些学生选修了,课程没有被选修的直接显示NULL
自连接:
对于一个表自己的多个字段进行连接查询
Usage:
SELECT c.Name AS student,s.Name AS teacher FROM students AS c,students AS s WHERE
c.TID=s.SID;
查看students表中TID和SID相同的字段,并显示他们的学生名和老师名。AS取别名
子查询:
查询语句里面嵌套其他的子查询,比较操作中使用子查询,子查询只能返回单个值;
Usage: SELECT Name FROM students WHERE Age > (SELECT AVG(Age) FROM students);
查询年龄大于平均年龄的学生姓名
IN (): 使用子查询
Usage:SELECT Name FROM students WHERE Age IN(SELECT Age FROM teacheers);
查询学生年龄中与老师年龄一样的学生姓名,IN表示子查询可以是一组数值。
FROM中也可以插入子查询:
Usage:SELECT Name,Age FROM (SELECT Name,Age FROM students) AS t WHERE t.Age >= 20;
从SELECT查询的结构的表中再从中查询其他符合条件的数据
联合查询:
UNION :把两张表连接成一张表查询显示出来
Usage: (SELECT Name,Age FROM class) UNION (SELECT CID,Couse FROM courses)
把courses表中CID和Couse字段跟class表中的Name和Age字段联合起来显示为一张表
mysql> SELECT Name,Age FROM class;
+-------------+-----+
| Name | Age |
+-------------+-----+
| Yang Guo | 22 |
| Guo Jing | 46 |
| Xiao Longnv | 18 |
| Huang Rong | 40 |
+-------------+-----+
4 rows in set (0.00 sec)
mysql> SELECT CID,Couse FROM courses;
+-----+-----------+
| CID | Couse |
+-----+-----------+
| 1 | physics |
| 2 | english |
| 3 | chemistry |
| 4 | maths |
+-----+-----------+
4 rows in set (0.00 sec)
mysql> (SELECT Name,Age FROM class) UNION (SELECT CID,Couse FROM courses);
+-------------+-----------+
| Name | Age |
+-------------+-----------+
| Yang Guo | 22 |
| Guo Jing | 46 |
| Xiao Longnv | 18 |
| Huang Rong | 40 |
| 1 | physics |
| 2 | english |
| 3 | chemistry |
| 4 | maths |
+-------------+-----------+
8 rows in set (0.00 sec)
实例:
1.挑选出courses表中没有被students表中的CID2学习的课程的课程名称;
msyql> SELECT Cname FROM courses WHERE CID NOT IN (SELECT DISTINCT CID2 FROM students
WHERE CID2 IS NOT NULL); #SELECT DISTINCT CID2 FROM students WHERE CID2 IS NOT NULL从students表中找出CID2不为空的且不重复的行,然后从courses表中找到CID不在刚才查找的那些行
里面的Cname的值
2.找出students表中CID2有两个或者以上的同学学习了的,同一门课程的课程名称;
msyql> SELECT Cname FROM courses WHERE CID IN (SELECT CID2 FROM students GROUP
BY CID2 HAVING COUNT(CID2) >= 2); #查找一门课程至少2个同学学习,并显示课程名称。
3.显示每一个课程及其相关的老师,没有老师教授的课程将其老师显示为空;
msyql> SELECT t.Tname,c.Cname FROM teachers AS t RIGHT JOIN courses AS c ON t.CID=c.TID;
4.显示每一位老师及其所教授的课程,没有教授的课程保持为null;
msyql> SELECT t.Tname,c.Cname FROM teachers AS t LEFT JOIN courses AS c ON t.CID=c.TID;
5.显示每位同学的CID1课程名及其讲授了相关课程的老师名称;
msyql> SELECT Name,Cname,Tname FROM students,courses,teachers WHERE students.CID1=courses.CID AND courses.CID=teachers.CID;
看了以上介绍SELECT单表、多表查询升级及插入删除,希望能给大家在实际运用中带来一定的帮助。本文由于篇幅有限,难免会有不足和需要补充的地方,大家可以继续关注创新互联行业资讯板块,会定期给大家更新行业新闻和知识,如有需要更加专业的解答,可在官网联系我们的24小时售前售后,随时帮您解答问题的。