mysql找到同一个表中两行之间的差异,并且只显示第二行差异而不是第一行

时间:2016-08-28 19:37:41

标签: mysql sql

CREATE TABLE IF NOT EXISTS `project_details` (
  `project_name` varchar(100) NOT NULL,
  `project_detail` varchar(100) NOT NULL,
  `project_version` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `project_details` (`project_name`, `project_detail`, `project_version`) VALUES
('project_1', 'php_mysql', '1.0'),
('project_1', 'php_mysql_android', '2.0'),
('project_1', 'php_mysql_android_ajax', '3.0'),
('project_2', 'php_mysql', '1.0'),
('project_2', 'php_mysql_android', '2.0'),
('project_2', 'php_mysql_android_ajax', '3.0'),
('project_2', 'php_mysql_jquery', '1.0'),
('project_2', 'php_mysql', '4.0');

这就是表格的样子

enter image description here

通过将同一个表中的两行与下面的查询进行比较

select *
from project_details p1
where not exists (
select 1
from project_details p2
where p1.project_name <> p2.project_name
and p1.project_detail = p2.project_detail
and p1.project_version = p2.project_version
)

成功地获得了project_1和project_2的输出差异,就像这样

enter image description here

现在,如果我像这样更改表中的记录

CREATE TABLE IF NOT EXISTS `project_details` (
  `project_name` varchar(100) NOT NULL,
  `project_detail` varchar(100) NOT NULL,
  `project_version` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `project_details` (`project_name`, `project_detail`, `project_version`) VALUES
('project_1', 'php_mysql', '1.0'),
('project_1', 'php_mysql_android', '2.0'),
('project_1', 'php_mysql_android_ajaxs', '4.0'),
('project_2', 'php_mysql', '1.0'),
('project_2', 'php_mysql_android', '2.0'),
('project_2', 'php_mysql_android_ajax', '3.0'),
('project_2', 'php_mysql_jquery', '1.0'),
('project_2', 'php_mysql', '4.0'); 

其中记录已更改的表格如下所示

enter image description here

使用与上面相同的比较查询,比较project_1和project_2,我可以像这样获得输出

enter image description here

但在这里我只想显示project_2差异。我不想显示project_1差异

更简单:我想比较project_1和project_2的两行,但我想仅显示project_2差异,不需要显示project_1差异信息

That is I need to show some thing like comparing with project_1 what all are difference found in project_2 

我需要对此进行查询,在此先感谢

1 个答案:

答案 0 :(得分:1)

您需要的只是一个附加条件:

p1.project_name = 'project_2'

http://sqlfiddle.com/#!9/035126/4

但是你可以做得更好,并选择&#39; project_1&#39;之间的所有版本差异。和&#39; project_2&#39;:

select pd.project_detail, 
  p1.project_version as old_version,
  p2.project_version as new_version
from(
  select distinct project_detail
  from project_details
  where project_name in ('project_1', 'project_2')
) pd
left join project_details p1
  on  p1.project_name = 'project_1'
  and p1.project_detail = pd.project_detail
left join project_details p2
  on  p2.project_name = 'project_2'
  and p2.project_detail = pd.project_detail
where not (p1.project_version <=> p2.project_version)

结果如下:

|         project_detail | old_version | new_version |
|------------------------|-------------|-------------|
| php_mysql_android_ajax |         4.0 |         3.0 |
|              php_mysql |         1.0 |         4.0 |
|    somthing_depricated |         1.0 |      (null) |
|       php_mysql_jquery |      (null) |         1.0 |

http://sqlfiddle.com/#!9/f9005c/1