`

查询表中重复数据,按部门排序

 
阅读更多
DROP TABLE IF EXISTS `tempTb`;
CREATE TEMPORARY TABLE tempTb SELECT product_id,project_id,COUNT(*) AS num FROM product GROUP BY product_id, project_id HAVING num > 1 ORDER BY project_id;
DROP TABLE IF EXISTS MaxTb;
CREATE TEMPORARY TABLE MaxTb SELECT product_id,project_id,MAX(product_id) AS maxPdt FROM product
SELECT MAX(product_id),project_id FROM product 
SELECT product.`product_id`,product.`activity_id`,product.`project_id` FROM tempTb LEFT JOIN product ON product.`product_id` = tempTb.product_id AND product.`project_id` = tempTb.project_id;



DROP TABLE IF EXISTS maxTb;
CREATE TEMPORARY TABLE maxTb SELECT product_id,project_id,MAX(product_id) AS maxPdt FROM product GROUP BY project_id;
SELECT maxPdt FROM maxTb;


SELECT a.product_id,a.project_id,a.activity_id,COUNT(*) AS 序号
FROM product AS a ,product AS b
WHERE a.project_id=b.project_id AND   a.product_id<=b.product_id  AND a.`activity_id` >= b.`activity_id`
GROUP BY a.project_id,a.`activity_id`


DROP TABLE IF EXISTS product_temp;
CREATE TEMPORARY TABLE product_temp 
SELECT project_id,activity_id,product_id,count(*) AS num FROM product GROUP BY product_id,project_id HAVING num > 1 ORDER BY project_id,activity_id;

SELECT product.product_id,product.activity_id,product.project_id from product_temp 
LEFT JOIN product ON product_temp.project_id = product.project_id AND product_temp.product_id = product.product_id;

-- 进行过滤查询,查询多余的去掉最小activity_id那条记录
select product_id,activity_id,project_id from product a
where (a.product_id,a.project_id) in (select product_id,project_id from product group by product_id,project_id having count(*) > 1)
and (product_id,project_id,activity_id) not in (select product_id,project_id,min(activity_id) from product group by product_id,project_id having count(*)>1)

-- 排序操作
set @a1='';
set @b1=1;
select project_id,activity_id,
       if(@a1='',@b1,if(@a1=project_id,@b1:=@b1+1,@b1:=1)),
       @a1:=project_id 
  from product 
  order by project_id,activity_id desc;
  
 --序号
set @a1='';
set @b1=1;
select project_id,activity_id,product_id,
       if(@a1='',@b1,if(@a1=project_id,@b1:=@b1+1,@b1:=1)) AS nob,
       @a1:=project_id AS prj_id,
				(select MAX(product_id) from product GROUP BY @al)
  from product 
  order by project_id,activity_id desc; 
  
  -- 过滤掉重复的数据
  set @a1='';
set @b1=1;
select product_id,activity_id,project_id,
       if(@a1='',@b1,if(@a1=project_id,@b1:=@b1+1,@b1:=1)) AS nob,
       @a1:=project_id AS prj_id
from 
(
select product_id,activity_id,project_id from product a
where (a.product_id,a.project_id) in (select product_id,project_id from product group by product_id,project_id having count(*) > 1)
and (product_id,project_id,activity_id) not in (select product_id,project_id,min(activity_id) from product group by product_id,project_id having count(*)>1)
) AS product_ta
order by project_id,activity_id desc;

-- 创建临时表,更新数据库
DROP TABLE IF EXISTS product1_temp;
CREATE TABLE product1_temp SELECT * FROM product1;
UPDATE product1 SET product1.product_id = 
(SELECT product1_temp.product_id-product1_temp.project_id FROM product1_temp 
WHERE product1.project_id = product1_temp.project_id
AND product1.activity_id = product1_temp.activity_id);  

--将最大值融合进去
set @b1=1;
select product_id,activity_id,project_id,maxPrd,
       if(@a1='',@b1,if(@a1=project_id,@b1:=@b1+1,@b1:=1)) AS nob,
       @a1:=project_id AS prj_id
from 
(
select product_id,activity_id,a.project_id,maxPrdTb.maxPrd
from product AS a 
LEFT JOIN (SELECT project_id,MAX(product_id) AS maxPrd FROM product GROUP BY project_id) AS maxPrdTb
ON a.project_id = maxPrdTb.project_id
where (a.product_id,a.project_id) in (select product.product_id,product.project_id from product group by product.product_id,product.project_id having count(*) > 1)
and (a.product_id,a.project_id,a.activity_id) not in (select product.product_id,product.project_id,min(product.activity_id) from product group by product.product_id,product.project_id having count(*)>1)
) AS product_ta
order by project_id,activity_id ASC;

set @a1='';
set @b1=1;
select product_id,activity_id,project_id,maxPrd,
       if(@a1='',@b1,if(@a1=project_id,@b1:=@b1+1,@b1:=1)) + maxPrd AS newPrdId,
       @a1:=project_id AS prj_id
from 
(
select product_id,activity_id,a.project_id,maxPrdTb.maxPrd
from product AS a 
LEFT JOIN (SELECT project_id,MAX(product_id) AS maxPrd FROM product GROUP BY project_id) AS maxPrdTb
ON a.project_id = maxPrdTb.project_id
where (a.product_id,a.project_id) in (select product.product_id,product.project_id from product group by product.product_id,product.project_id having count(*) > 1)
and (a.product_id,a.project_id,a.activity_id) not in (select product.product_id,product.project_id,min(product.activity_id) from product group by product.product_id,product.project_id having count(*)>1)
) AS product_ta
order by project_id,activity_id ASC;
  
  

 进行整合查询

set @a1='';
set @b1=1;
DROP TABLE IF EXISTS my_product;
CREATE TEMPORARY TABLE my_product
select product_id,activity_id,project_id,maxPrd,
       if(@a1='',@b1,if(@a1=project_id,@b1:=@b1+1,@b1:=1)) + maxPrd AS newPrdId,
       @a1:=project_id AS prj_id
from 
(
select product_id,activity_id,a.project_id,maxPrdTb.maxPrd
from product AS a 
LEFT JOIN (SELECT project_id,MAX(product_id) AS maxPrd FROM product GROUP BY project_id) AS maxPrdTb
ON a.project_id = maxPrdTb.project_id
where (a.product_id,a.project_id) in (select product.product_id,product.project_id from product group by product.product_id,product.project_id having count(*) > 1)
and (a.product_id,a.project_id,a.activity_id) not in (select product.product_id,product.project_id,min(product.activity_id) from product group by product.product_id,product.project_id having count(*)>1)
) AS product_ta
order by project_id,activity_id ASC;
SELECT * FROM my_product;

 

以下是数据库的图片


#mysql中两张表数据的部分更新,用tb2的部分数据更新tb1
UPDATE tb1,tb2 SET tb1.address=tb2.address WHERE tb1.name=tb2.name

 
 exists去重

SELECT
*
FROM
表 main
WHERE
NOT EXISTS (
SELECT * FROM 表 sub
WHERE main.division = sub.division AND main.district > sub.district
)

 

  • 大小: 12.9 KB
分享到:
评论

相关推荐

    SQL Server数据库实验_数据查询与更新_简单的单表查询.doc

    在SQL Server数据库中,数据查询与更新是数据库操作的基础,本实验主要关注如何使用SQL语句进行简单的单表查询。实验旨在让学生熟练掌握SELECT语句的使用,包括SELECT子句、FROM子句、WHERE子句以及ORDER BY子句,...

    数据透视表与SQL典型应用

    数据透视表是数据分析领域中的一个强大工具,常用于整理、汇总和分析大量数据。它能够帮助用户快速地从不同角度观察数据,进行多维度的统计分析。SQL(Structured Query Language),结构化查询语言,是数据库管理和...

    Excel模板人事信息查询表.zip

    在描述中,“Excel模板人事信息查询表.zip”重复出现,进一步强调了这个模板是用来处理人事信息的,可能包含了员工的基本信息、考勤记录、薪资数据等。Excel是一款由Microsoft开发的电子表格程序,常用于数据分析、...

    经典表关联与多表查询

    经典表关联与多表查询 经典表关联与多表查询是数据库...(4)自连接可以用于查询表中是否有重复的记录。 经典表关联与多表查询是数据库系统中非常重要的概念,掌握它可以让你更好地应用于实际的数据库管理和开发中。

    SQL语句与数据透视表结合使用入门

    例如,SQL可以用于计算每个部门的总销售额,而数据透视表则可进一步展示按产品分类的销售详情。 4. **动态数据透视表**:如果数据经常更新,可以通过SQL脚本自动化更新数据源,保证数据透视表始终反映最新信息。 ...

    插入排序-18-右连接查询.ev4.rar

    - 右连接在数据分析和报告生成中特别有用,例如,当需要查看所有员工及其对应的部门信息时,即使有些员工尚未分配到任何部门(在部门表中没有对应记录),右连接可以确保显示所有员工,即使他们的部门信息为NULL。...

    大学数据库原理实验答案--数据查询

    在这个实验中,我们使用SELECT语句来选择DEPT表中的部门号和部门名称,然后使用ORDER BY子句来按部门名称排序。 4. 显示所有不同的工作类型(工种)。 知识点:DISTINCT关键字的使用、SELECT语句的使用 在这个...

    SQL练习题网络数据库

    以上知识点涵盖了在数据库中使用SQL进行数据查询、排序、分组、子查询、数据操作等基础而重要的操作。这些操作是数据库管理和开发人员必备的技能。通过练习上述的SQL练习题,可以加深对SQL语言的理解和应用能力。...

    oracle 查询出每组中最大的三个记录和查询重复记录

    在Oracle数据库中,查询数据是日常操作的一部分,而有时候我们需要获取特定的数据集,例如每组中的最大几个记录或者找出重复的记录。这篇博客主要探讨如何实现这两个目标。 首先,我们来讨论如何查询每组中最大的三...

    SQLserver数据查询[归类].pdf

    本章主要介绍了如何使用SQL语句进行基本的数据查询,包括从单个或多个表中提取所需信息。以下是对这些基本查询概念和示例的详细解释: 6.1 基本查询: SQL数据查询的基础是SELECT语句,通常采用SELECT-FROM-WHERE...

    公司管理数据库系统的数据查询.ppt

    1. **SQL查询语句基础**:在数据库管理中,SELECT语句是最基本的数据检索工具,它允许从一个或多个表中选择所需的数据。SQL查询语句的基本结构包括SELECT、INTO、FROM、WHERE、GROUP BY、HAVING和ORDER BY子句。 2....

    MySql基本查询、连接查询、子查询、正则表达查询讲解

    查询数据是指从数据库中的数据表或视图中获取所需要的数据,在mysql中,可以使用SELECT语句来查询数据。根据查询条件的不同,数据库系统会找到不同的数据。 SELECT语句的基本语法格式如下: [sql] view plain copy ...

    SQL编辑维护表格数据

    使用`INSERT INTO`语句将数据添加到表中。例如,向`Employees`表中插入一条记录: ``` INSERT INTO Employees (ID, Name, Position) VALUES (1, '张三', '经理'); ``` 3. **查询数据**: `SELECT`语句用于检索...

    根据Oracle数据库scott模式下的emp表和dept表

    5. 查询 10 号部门中工种为 MANAGER 和 20 号部门中工种为 CLERK 的员工的信息 select * from emp where (deptno = 10 and job like 'MANAGER') or (deptno = 20 and job like 'CLERK'); 这个查询语句使用了逻辑...

    计算机二级Access模拟试题

    - **题目**:在Access中,从表中访问数据要比从查询中快得多,如果经常要从几个表中提取数据,那么最好的方法是使用一种操作查询,这种操作查询是? - **答案**:B) 生成表查询 - **解析**:生成表查询可以创建一...

    数据库实验报告——数据库的简单查询和连接查询

    实验的目的是让学生掌握SQL Server查询分析器的使用,熟悉SQL查询语句,特别是针对数据表的查询操作,如数据选择、排序和连接。实验要求学生在实验前预习并编写SQL语句,实验中独立完成查询任务,遇到问题时在教师...

    数据库管理系统MySQL实验4.doc

    * 查询 Employees 表中部门号和性别,使用 DISTINCT 消除重复行 * 查询月收入高于 2000 的员工号码 * 查询所有 1970 以后出生的员工的姓名和住址 * 查询所有财务部门的员工号码和姓名 * 查询 Employees 表中男员工的...

Global site tag (gtag.js) - Google Analytics