当前位置:Gxlcms > 数据库问题 > MySQL的连接查询

MySQL的连接查询

时间:2021-07-01 10:21:17 帮助过:16人阅读

MySQL的连接查询

  • 建表

    1.一对一关系

    2.一对多、多对一关系

    3.多对多关系(需要建立一张关联表,进行二者的关联)

    4.例:订单管理,用户下订单,一个用户可以下一个或多个订单,一个订单可能包含一个或多个商品。

  • 例:以下 sql 语句都依据此数据库

    1. <code> /*
    2. Navicat MySQL Data Transfer
    3. Source Server : localhost_3306
    4. Source Server Version : 50528
    5. Source Host : localhost:3306
    6. Source Database : moretable
    7. Target Server Type : MYSQL
    8. Target Server Version : 50528
    9. File Encoding : 65001
    10. Date: 2018-12-09 10:54:25
    11. */
    12. SET FOREIGN_KEY_CHECKS=0;
    13. -- ----------------------------
    14. -- Table structure for cla
    15. -- ----------------------------
    16. DROP TABLE IF EXISTS `cla`;
    17. CREATE TABLE `cla` (
    18. `classid` int(11) NOT NULL,
    19. `classname` varchar(255) DEFAULT NULL,
    20. `classtime` int(11) DEFAULT NULL,
    21. PRIMARY KEY (`classid`)
    22. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    23. -- ----------------------------
    24. -- Records of cla
    25. -- ----------------------------
    26. INSERT INTO `cla` VALUES (‘1‘, ‘数学‘, ‘10‘);
    27. INSERT INTO `cla` VALUES (‘2‘, ‘语文‘, ‘18‘);
    28. INSERT INTO `cla` VALUES (‘3‘, ‘音乐‘, ‘12‘);
    29. INSERT INTO `cla` VALUES (‘4‘, ‘美术‘, ‘18‘);
    30. INSERT INTO `cla` VALUES (‘5‘, ‘体育‘, ‘15‘);
    31. INSERT INTO `cla` VALUES (‘6‘, ‘计算机‘, ‘32‘);
    32. INSERT INTO `cla` VALUES (‘7‘, ‘英语‘, ‘36‘);
    33. INSERT INTO `cla` VALUES (‘8‘, ‘Java‘, ‘20‘);
    34. INSERT INTO `cla` VALUES (‘9‘, ‘数据库‘, ‘40‘);
    35. INSERT INTO `cla` VALUES (‘10‘, ‘建筑‘, ‘20‘);
    36. -- ----------------------------
    37. -- Table structure for stu
    38. -- ----------------------------
    39. DROP TABLE IF EXISTS `stu`;
    40. CREATE TABLE `stu` (
    41. `stuid` int(11) NOT NULL,
    42. `stuname` varchar(255) DEFAULT NULL,
    43. `stuage` varchar(255) DEFAULT NULL,
    44. `stusex` varchar(255) DEFAULT NULL,
    45. PRIMARY KEY (`stuid`)
    46. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    47. -- ----------------------------
    48. -- Records of stu
    49. -- ----------------------------
    50. INSERT INTO `stu` VALUES (‘1‘, ‘小明‘, ‘20‘, ‘男‘);
    51. INSERT INTO `stu` VALUES (‘2‘, ‘小华‘, ‘21‘, ‘男‘);
    52. INSERT INTO `stu` VALUES (‘3‘, ‘小红‘, ‘19‘, ‘男‘);
    53. INSERT INTO `stu` VALUES (‘4‘, ‘小美‘, ‘19‘, ‘女‘);
    54. INSERT INTO `stu` VALUES (‘5‘, ‘小爱‘, ‘20‘, ‘女‘);
    55. INSERT INTO `stu` VALUES (‘6‘, ‘小风‘, ‘19‘, ‘男‘);
    56. INSERT INTO `stu` VALUES (‘7‘, ‘小太‘, ‘22‘, ‘女‘);
    57. INSERT INTO `stu` VALUES (‘8‘, ‘小齐‘, ‘25‘, ‘男‘);
    58. INSERT INTO `stu` VALUES (‘9‘, ‘小镇‘, ‘24‘, ‘女‘);
    59. INSERT INTO `stu` VALUES (‘10‘, ‘小星‘, ‘23‘, ‘女‘);
    60. INSERT INTO `stu` VALUES (‘11‘, ‘小刚‘, ‘25‘, ‘男‘);
    61. INSERT INTO `stu` VALUES (‘12‘, ‘小哈‘, ‘22‘, ‘女‘);
    62. INSERT INTO `stu` VALUES (‘13‘, ‘文华‘, ‘26‘, ‘男‘);
    63. INSERT INTO `stu` VALUES (‘14‘, ‘小易‘, ‘22‘, ‘女‘);
    64. INSERT INTO `stu` VALUES (‘15‘, ‘小改‘, ‘22‘, ‘女‘);
    65. INSERT INTO `stu` VALUES (‘16‘, ‘小星‘, ‘23‘, ‘男‘);
    66. INSERT INTO `stu` VALUES (‘17‘, ‘小文‘, ‘15‘, ‘未知‘);
    67. INSERT INTO `stu` VALUES (‘18‘, ‘小文‘, ‘20‘, ‘女‘);
    68. -- ----------------------------
    69. -- Table structure for stu_cla
    70. -- ----------------------------
    71. DROP TABLE IF EXISTS `stu_cla`;
    72. CREATE TABLE `stu_cla` (
    73. `id` int(11) NOT NULL,
    74. `stuid` int(11) DEFAULT NULL,
    75. `claid` int(11) DEFAULT NULL,
    76. PRIMARY KEY (`id`)
    77. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    78. -- ----------------------------
    79. -- Records of stu_cla
    80. -- ----------------------------
    81. INSERT INTO `stu_cla` VALUES (‘1‘, ‘1‘, ‘1‘);
    82. INSERT INTO `stu_cla` VALUES (‘2‘, ‘2‘, ‘4‘);
    83. INSERT INTO `stu_cla` VALUES (‘3‘, ‘3‘, ‘3‘);
    84. INSERT INTO `stu_cla` VALUES (‘4‘, ‘4‘, ‘4‘);
    85. INSERT INTO `stu_cla` VALUES (‘5‘, ‘5‘, ‘2‘);
    86. INSERT INTO `stu_cla` VALUES (‘6‘, ‘6‘, ‘6‘);
    87. INSERT INTO `stu_cla` VALUES (‘7‘, ‘7‘, ‘9‘);
    88. INSERT INTO `stu_cla` VALUES (‘8‘, ‘8‘, ‘6‘);
    89. INSERT INTO `stu_cla` VALUES (‘9‘, ‘10‘, ‘8‘);
    90. INSERT INTO `stu_cla` VALUES (‘10‘, ‘16‘, ‘9‘);</code>
  • sql 中什么时候用内连接查询,什么时候用外连接查询?

    需要查找两张表同时存在的数据,使用 内连接 ;需要查找两张表中一张表存在,另一张表不存在的时候使用左外链接右外链接


    例:内连接的查询结果都是满足连接条件的元组。
    但有时我们也希望输出那些不满足连接条件的元组信息。
    比如,我们想知道每个学生的选课情况,包括已经选课的学生(这部分学生的学号在学生表中有,在选课表中也有,是满足连接条件的),也包括没有选课的学生(这部分学生的学号在学生表中有,但在选课表中没有,不满足连接条件),这时就需要使用外连接。外连接是只限制一张表中的数据必须满足连接条件,而另一张表中的数据可以不满足连接条件的连接方式。

  • 内连接

    1. <code> #查询已经选过课的学生的选课情况
    2. SELECT
    3. s.stuid,s.stuname,s.stuage,sc.claid
    4. FROM
    5. stu_cla AS sc
    6. INNER JOIN
    7. stu AS s
    8. ON
    9. sc.stuid = s.stuid
    10. #查询已经选过课的学生以及所选择课程的信息
    11. SELECT
    12. s.stuid,s.stuname,s.stuage,c.classname,c.classtime
    13. FROM
    14. stu_cla AS sc
    15. INNER JOIN
    16. stu AS s
    17. ON
    18. sc.stuid = s.stuid
    19. INNER JOIN
    20. cla AS c
    21. ON
    22. sc.claid = c.classid</code>
  • 左连接

    1. <code> #查询所有学生的选课情况
    2. select
    3. s.stuid,s.stuname,s.stuage,s.stusex,sc.claid
    4. FROM
    5. stu as s
    6. LEFT JOIN
    7. stu_cla AS sc
    8. ON
    9. sc.stuid = s.stuid
    10. #查询所有学生选课情况及课程信息
    11. select
    12. s.stuid,s.stuname,s.stuage,s.stusex,c.classname,c.classtime
    13. FROM
    14. stu as s
    15. LEFT JOIN
    16. stu_cla AS sc
    17. ON
    18. sc.stuid = s.stuid
    19. LEFT JOIN
    20. cla AS c
    21. ON
    22. sc.claid = c.classid
    23. #查询 stuid=1 的学生选课情况
    24. select
    25. s.stuid,s.stuname,s.stuage,s.stusex,c.classname,c.classtime
    26. FROM
    27. stu as s
    28. LEFT JOIN
    29. stu_cla AS sc
    30. ON
    31. sc.stuid = s.stuid
    32. LEFT JOIN
    33. cla AS c
    34. ON
    35. sc.claid = c.classid
    36. WHERE
    37. s.stuid = 1</code>
  • 模糊查询

    1. <code> SELECT * FROM stu WHERE stuname LIKE ‘%三%‘ </code>
  • concat 函数(连接字符串)

    1. <code> select concat (stuid, stuname, stuage) as info from stu;
    2. #在mybatis中的应用,用来拼接模糊查询字符串
    3. select * from stu where stuname like concat(‘%‘,#{stuname},‘%‘)</code>
  • 分组查询

    1. <code> #按性别分组查年龄最大值
    2. select stuname,max(stuage),stusex from stu group by stusex; </code>
  • 去重查询
    1.distinct必须放在最开头

    2.distinct只能使用需要去重的字段进行操作。也就是说我sidtinct了name,age两个字段,我后面想根据id进行排序,是不可以的,因为只能name,age两个字段进行操作.

    3.distinct去重多个字段时,含义是:几个字段 同时重复 时才会被 过滤。

    1. <code> #查询名字不相同的学生;
    2. select distinct stuname from stu;
    3. #查询名字和年龄同时不同的学生
    4. select distinct stuname,stusex from stu;</code>
  • count 函数

    Count(1)和Count( * )实际上的意思是,评估Count()中的表达式是否为NULL,如果为NULL则不计数,而非NULL则会计数。比如我们看代码1所示,在Count中指定NULL(优化器不允许显式指定NULL,因此需要赋值给变量才能指定)。
    因此当你指定Count( * ) 或者Count(1)或者无论 Count(‘anything’) 时结果都会一样,因为这些值都不为NULL。

    1. <code> select count(1) from stu;
    2. select count(*) from stu;
    3. select count(‘hgjkyh‘) from stu;
    4. #起别名 (as)
    5. select count(‘hgjkyh‘) as 总数 from stu;
    6. #分组计数
    7. select stusex as 性别,count(1)as 总数 from stu GROUP BY stusex;</code>

MySQL的连接查询

标签:navicat   管理   inno   uname   评估   部分   用户   arch   连接查询   

人气教程排行