2012-08-10 99 views
2

我们有一个表像,Mysql的内部连接的两个子查询返回重复的行

Table1(Contract_id, name, address, contact_no) 

和另一个表像,

Table2(Contract_id, approver, owner, authority) 

的样本数据:

mysql> select * from Table1; 
+-------------+-------+---------+------------+ 
| contract_id | owner | address | contact_no | 
+-------------+-------+---------+------------+ 
|  11111 | XXX | Madurai | 897161  | 
|  12456 | XYZ | Madras | 897161  | 
|  11111 | XYZ | Madras | 897161  | 
+-------------+-------+---------+------------+ 
3 rows in set (0.00 sec) 

mysql> select * from Table2; 
+-------------+----------+ 
| contract_id | approver | 
+-------------+----------+ 
|  11111 | YZX  | 
|  11112 | YYY  | 
+-------------+----------+ 
2 rows in set (0.00 sec) 

我已经写一个查询来获得所有contract_id和匹配数据的这样的标准,

“获取的所有合同与业主喜欢‘X’,解决像‘疯狂’,批准=‘YZX’”

select contract_id,owner,address,approver 
from 
(
    select * 
    from Table1 
    where owner like '%X%' 
    and address like '%Mad%' 
) t1 
inner join 
(
    select * 
    from Table2 
    where approver = 'YZX' 
) t2 using (contract_id); 

它返回正确的结果。但问题是左表有两个匹配的行,右表只有一个匹配的行。所以右表中的行重复两次。

> +-------------+---------------+-----------+-------------+ 
> |contract_id | owner  | address | approver | 
> +-------------+---------------+-----------+-------------+ 
> |11111  | XXX   | Madurai | YZX   | 
> +-------------+---------------+-----------+-------------+ 
> |11111  | XYZ   | Madras | YZX   | 
> +-------------+---------------+-----------+-------------+ 

批准者值被复制两次。我可以以某种方式避免这个mysql本身? 我希望为第二行的批准人列添加空值。

编辑1:

我编辑了我的查询通过审批在年底拥有组,

select contract_id,owner,address,approver 
from 
(
    select contract_id 
    from Table1 
    where owner like '%X%' 
    and address like '%Mad%' 
) t1 
inner join 
(
    select contract_id 
    from Table2 
    where approver = 'YZX' 
) t2 using (contract_id) group by approver; 

现在的结果变得像,

> +-------------+---------------+-----------+-------------+ 
> |contract_id | owner  | address | approver | 
> +-------------+---------------+-----------+-------------+ 
> |11111  | XYZ   | Madurai | YZX   | 
> +-------------+---------------+-----------+-------------+ 

第二走到现在下落不明。我也想要。

编辑2:添加了示例数据和精确查询。

编辑3:我想要的结果要格式化像下面的,

> +-------------+---------------+-----------+-------------+ 
> |contract_id | owner  | address | approver | 
> +-------------+---------------+-----------+-------------+ 
> |11111  | XXX   | Madurai | YZX   | 
> +-------------+---------------+-----------+-------------+ 
> |11111  | XYZ   | Madras | NULL  | 
> +-------------+---------------+-----------+-------------+ 

编辑4:我来达到的什么我想要的。以下是我使用的查询。

create temporary table table1 select * 
from 
(
    select * 
    from Table1 
    where owner like '%X%' 
    and address like '%Mad%' 
) t1 
inner join 
(
    select * 
    from Table2 
    where approver = 'YZX' 
) t2 using (contract_id) limit 1; 

和查询,

insert into table1 select contract_id, IF((select count(*) from table1 where owner = t.owner and contract_id = t.contract_id) > 0, NULL, t.contract_id),IF((select count(*) from table1 where address = t.adress and contract_id = t.contract_id) > 0, NULL, t.contract_id),IF((select count(*) from table1 where approver = t.approver and contract_id = t.contract_id) > 0, NULL, t.contract_id) 
from 
((
    select * 
    from Table1 
    where owner like '%X%' 
    and address like '%Mad%' 
) t1 
inner join 
(
    select * 
    from Table2 
    where approver = 'YZX' 
) t2 using (contract_id)) t; 
+0

你可以发布一些示例数据为两个表? – Taryn 2012-08-10 11:48:23

回答

3

只是为了清楚数据不重复。由于您正在加入contract_id上的两个表格,并且相同的合同ID与两条记录相关联,因此您将有批准者出现两次。

mysql> select * from Table1; 
+-------------+-------+---------+------------+ 
| contract_id | owner | address | contact_no | 
+-------------+-------+---------+------------+ 
|  11111 | XXX | Madurai | 897161  | -- contract_id 11111 
|  12456 | XYZ | Madras | 897161  | 
|  11111 | XYZ | Madras | 897161  | -- contract_id 11111 
+-------------+-------+---------+------------+ 
3 rows in set (0.00 sec) 

mysql> select * from Table2; 
+-------------+----------+ 
| contract_id | approver | 
+-------------+----------+ 
|  11111 | YZX  | -- contract_id 11111 
|  11112 | YYY  | 
+-------------+----------+ 
2 rows in set (0.00 sec) 

所以结果总是从表1的两个记录,并从表中的两个与approver = 'YZX'

在另一方面的一个记录,我可能会考虑重写查询到以下几点:

select t1.contract_id, t1.owner, t1.address, t2.approver 
from table1 t1 
inner join table2 t2 
    on t1.contract_id = t2.contract_id 
where t1.owner like '%X%' 
    and t1.address like '%Mad%' 
    and t2.approver = 'YZX' 

根据你的评论,我开始认为你可能想要一个LEFT JOIN

select t1.contract_id, t1.owner, t1.address, t2.approver 
from table1 t1 
left join table2 t2 
    on t1.contract_id = t2.contract_id 
    and t2.approver = 'YZX' 
where t1.owner like '%X%' 
    and t1.address like '%Mad%' 

请参阅SQL Fiddle with Demo

编辑2:没有简单的方法强制使用空值作为附加字段。您可以尝试使用ROWNUMBER得到的结果:

select x.contract_id, 
    x.owner, 
    x.address, 
    case when (@rownum:[email protected]+1 = 1) then x.approver else null end approver 
from 
(
    select t1.contract_id, t1.owner, t1.address, t2.approver 
    from table1 t1 
    inner join table2 t2 
    on t1.contract_id = t2.contract_id 
    and t2.approver = 'YZX' 
    where t1.owner like '%X%' 
    and t1.address like '%Mad%' 
) x, (SELECT @rownum:=0) r 

SQL Fiddle with Demo

+0

contract_id是否相同?在table2上?两行有不同的ID只有没有? – tamizhgeek 2012-08-10 12:14:26

+0

'Table2'有一个'contract_id',等于'Table1'中的结果。 – Taryn 2012-08-10 12:16:50

+0

@tamizhgeek看到我的编辑,我想你可能想要一个'LEFT JOIN' – Taryn 2012-08-10 12:18:40

1

使用DISTINCT子句以避免重复:

SELECT contract_id,owner,address,approver 
from (select DISTINCT contract_id 
     from Table1 where owner like '%X%' and address like '%Mad%' 
    ) t1 
    inner join (select contract_id from Table2 where approver = 'YZX') t2 
     using (contract_id); 
+0

不幸运! DISTINCT仍然给出相同的结果。 – tamizhgeek 2012-08-10 11:47:54

1

它不重复,它显示正是它应该显示。

如果你解释了你想要显示哪条记录的条件以及你想从结果集中排除哪条记录,那么也许我们可以写一些符合你的要求的记录。

您在第一次更新中提供的查询在MySQL上可能在语法上有效(它不适用于其他数据库),但是语义上是胡言乱语 - 您是如何告诉DBMS您确实想要排除owner = XXX?或地址=马德拉斯?

+0

我的查询很简单。可能我已经过于复杂了。责怪我的坏英语和我的公司不允许展示真实的数据。我在两张桌子上有一个共同的专栏。我想要将这些表加入该列并选择一些标准。我接受它只是返回数据。但是,有些如何使第二行的右表值显示为NULL? – tamizhgeek 2012-08-10 12:15:51

+0

然后,不要在输出中包含这些列(所有者,地址),并使用group by或difstinct修饰符。 – symcbean 2012-08-10 15:08:21