2012-08-10 97 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