• SQL 实例



     1 Date: 2020-08-24 16:07:35
     2 */
     3 
     4 SET FOREIGN_KEY_CHECKS=0;
     5 
     6 -- ----------------------------
     7 -- Table structure for websites
     8 -- ----------------------------
     9 DROP TABLE IF EXISTS `websites`;
    10 CREATE TABLE `websites` (
    11   `id` int(255) NOT NULL AUTO_INCREMENT,
    12   `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '网名',
    13   `url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '网址',
    14   `alexa` int(255) DEFAULT NULL COMMENT '排名',
    15   `country` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '城市',
    16   PRIMARY KEY (`id`)
    17 ) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
    18 
    19 -- ----------------------------
    20 -- Records of websites
    21 -- ----------------------------
    22 INSERT INTO `websites` VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA');
    23 INSERT INTO `websites` VALUES ('2', '淘宝', 'https://www.taobao.com/', '13', 'CN');
    24 INSERT INTO `websites` VALUES ('3', '菜鸟教程', 'http://www.runoob.com/', '4689', 'CN');
    25 INSERT INTO `websites` VALUES ('4', '微博 ', 'http://weibo.com/', '20', 'CN');
    26 INSERT INTO `websites` VALUES ('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');
    27 INSERT INTO `websites` VALUES ('7', 'stackoverflow', 'http://stackoverflow.com/', '0', 'IND');

     1 Date: 2020-08-24 15:58:02
     2 */
     3 
     4 SET FOREIGN_KEY_CHECKS=0;
     5 
     6 -- ----------------------------
     7 -- Table structure for access_log
     8 -- ----------------------------
     9 DROP TABLE IF EXISTS `access_log`;
    10 CREATE TABLE `access_log` (
    11   `aid` varchar(255) NOT NULL,
    12   `site_id` int(255) DEFAULT NULL,
    13   `count` int(255) DEFAULT NULL,
    14   `date` date DEFAULT NULL,
    15   PRIMARY KEY (`aid`)
    16 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    17 
    18 -- ----------------------------
    19 -- Records of access_log
    20 -- ----------------------------
    21 INSERT INTO `access_log` VALUES ('1', '1', '45', '2016-05-10');
    22 INSERT INTO `access_log` VALUES ('2', '3', '100', '2016-05-13');
    23 INSERT INTO `access_log` VALUES ('3', '1', '230', '2016-05-14');
    24 INSERT INTO `access_log` VALUES ('4', '2', '10', '2016-05-14');
    25 INSERT INTO `access_log` VALUES ('5', '5', '205', '2016-05-14');
    26 INSERT INTO `access_log` VALUES ('6', '4', '13', '2016-05-15');
    27 INSERT INTO `access_log` VALUES ('7', '3', '220', '2016-05-15');
    28 INSERT INTO `access_log` VALUES ('8', '5', '545', '2016-05-16');
    29 INSERT INTO `access_log` VALUES ('9', '3', '201', '2016-05-17');

    1 SELECT
    2     w.`name`,
    3     al.count,
    4     al.date
    5 FROM
    6     websites AS w
    7 LEFT JOIN access_log AS al ON w.id = al.site_id
    8 ORDER BY
    9     al.count DESC;

    By:LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL。

     原文链接转自:https://www.runoob.com/sql/sql-join-left.html


                      故屿γ                   

     

  • 相关阅读:
    微信支付的安全漏洞之XXE
    IP地址分类(A类 B类 C类 D类 E类)
    MySql新增表的字段,删除表字段
    Java基础之中间件的初识
    Java基础之IO框架
    微信H5支付坑一--手续费未结算
    设计模式之简单工厂模式
    nginx负载均衡的5种策略(转载)
    Mybatis注意点之#与$区别
    RSF 分布式 RPC 服务框架的分层设计
  • 原文地址:https://www.cnblogs.com/guyu-/p/13554604.html
Copyright © 2020-2023  润新知