我们有一张桌子,
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_ids 和匹配这样的标准的数据,
“获取与所有者的所有合同,例如“X”和地址,例如“Mad”和批准人 = '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;