We have a table like,
Table1(Contract_id, name, address, contact_no)
and another table like,
Table2(Contract_id, approver, owner, authority)
Sample data:
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)
I have written a query to get all contract_ids and matching data for a criteria like this,
"Get all contracts with Owner like 'X' and address like "Mad" and approver = '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);
It is returning the results correctly. But the problem is left table has two matching rows and right table has only one matching row. So the row in right table is duplicated twice.
> +-------------+---------------+-----------+-------------+
> |contract_id | owner | address | approver |
> +-------------+---------------+-----------+-------------+
> |11111 | XXX | Madurai | YZX |
> +-------------+---------------+-----------+-------------+
> |11111 | XYZ | Madras | YZX |
> +-------------+---------------+-----------+-------------+
The approver value is duplicated twice. Can i somehow avoid this mysql itself? I want null value for the second row's approver column.
EDIT 1:
I edited my query to have group by approver at the end,
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;
Now the results became like,
> +-------------+---------------+-----------+-------------+
> |contract_id | owner | address | approver |
> +-------------+---------------+-----------+-------------+
> |11111 | XYZ | Madurai | YZX |
> +-------------+---------------+-----------+-------------+
The second went missing now. I want that as well.
EDIT 2: Added the sample data and exact queries.
EDIT 3: I want the results to be formatted like the below,
> +-------------+---------------+-----------+-------------+
> |contract_id | owner | address | approver |
> +-------------+---------------+-----------+-------------+
> |11111 | XXX | Madurai | YZX |
> +-------------+---------------+-----------+-------------+
> |11111 | XYZ | Madras | NULL |
> +-------------+---------------+-----------+-------------+
EDIT 4: I acheived what I wanted. Below is the query I used.
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;
and the query,
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;