mysql的联表查询和去重复数据
生活随笔
收集整理的這篇文章主要介紹了
mysql的联表查询和去重复数据
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
mysql的聯(lián)表查詢和去重復(fù)數(shù)據(jù)
/* SQLyog Ultimate v10.00 Beta1 MySQL - 5.7.17-log : Database - pusmtnew ********************************************************************* *//*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=''*/;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`pnew` /*!40100 DEFAULT CHARACTER SET utf8 */;USE `pnew`;/*Table structure for table `student` */DROP TABLE IF EXISTS `student`;CREATE TABLE `student` (`id` int(10) NOT NULL,`name` varchar(30) DEFAULT NULL,`tid` int(10) DEFAULT NULL,PRIMARY KEY (`id`),KEY `fktid` (`tid`),CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;/*Table structure for table `t_status` */DROP TABLE IF EXISTS `t_status`;CREATE TABLE `t_status` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`serial_number` varchar(100) DEFAULT NULL,`location` varchar(100) DEFAULT NULL,`pin` varchar(30) DEFAULT NULL,`err_code` varchar(100) DEFAULT NULL,`repair_status` varchar(20) DEFAULT NULL,`image_filename` varchar(100) DEFAULT NULL,`algorithm` varchar(100) DEFAULT NULL,`sub_type` varchar(100) DEFAULT NULL,PRIMARY KEY (`id`),KEY `serial_number` (`serial_number`),CONSTRAINT `t_status_ibfk_1` FOREIGN KEY (`serial_number`) REFERENCES `tt_borders` (`serialNumber`) ) ENGINE=InnoDB AUTO_INCREMENT=12763666 DEFAULT CHARSET=utf8;/*Table structure for table `teacher` */DROP TABLE IF EXISTS `teacher`;CREATE TABLE `teacher` (`id` int(10) NOT NULL,`name` varchar(30) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;/*Table structure for table `tt_borders` */DROP TABLE IF EXISTS `tt_borders`;CREATE TABLE `tt_borders` (`serialNumber` varchar(50) NOT NULL,`boardType` varchar(100) DEFAULT NULL,`testerName` varchar(30) DEFAULT NULL,`testStatus` varchar(30) DEFAULT NULL,`testTime` timestamp NULL DEFAULT NULL,PRIMARY KEY (`serialNumber`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; SELECT serial_number,location,pin,err_code,repair_status,`algorithm`,sub_type FROM t_status WHERE repair_status='Repaired';SELECT serial_number,location,pin,err_code,repair_status,`algorithm`,sub_type ,COUNT(DISTINCT location) FROM t_status WHERE repair_status='Repaired' GROUP BY location ORDER BY serial_number;SELECT b.boardType,b.testerName,b.testTime,s.serial_number,location,pin,err_code,repair_status,`algorithm`,sub_type ,COUNT(DISTINCT location) FROM t_status sRIGHT OUTER JOIN tt_borders b ON s.serial_number = b.serialNumberWHERE s.repair_status='Repaired' AND b.testTime BETWEEN '2020-11-13 01:19:00' AND '2020-11-16 23:59:59' GROUP BY s.location ORDER BY s.serial_number;總結(jié)
以上是生活随笔為你收集整理的mysql的联表查询和去重复数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java做文件监控
- 下一篇: 掌握穷变富的12条原则 迅速从普通人变成