mysql如何找到同一个表中两行之间的差异并列出不匹配的记录? mysql在表中查找不匹配的行

时间:2016-08-27 23:54:22

标签: mysql sql

在MySQL中,我想找到同一个表中两行之间的差异 我需要显示不匹配的记录

这是我的名为project_details

的表
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

我想从表中获得不匹配记录的结果 这是来自比较project_1和project_2

需要比较来自project_name的project_1和project_2然后它应该获得project_detail和project_version的不匹配记录

我需要这样的结果,只显示不匹配的记录,请参阅下文 PIC

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

 select pd1.* from project_details as pd1 
     left join project_details as pd2 
     on (
       pd1.project_detail=pd2.project_detail and 
       pd1.project_version=pd2.project_version and
       pd1.project_name<>pd2.project_name)
     where pd2.project_name is null

答案 1 :(得分:1)

使用NOT EXISTS检索输出,对于不同的项目名称,根据详细信息和版本没有匹配的行:

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
  )

SQL Fiddle 了解它是如何运作的。

<强>输出

project_name    project_detail      project_version
---------------------------------------------------
project_2       php_mysql_jquery    1.0
project_2       php_mysql           4.0