我们有一个表像,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