我們有一個表像,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;
你可以發佈一些示例數據爲兩個表? – Taryn 2012-08-10 11:48:23