Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8357747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:34:50+00:00 2026-06-09T10:34:50+00:00

We have a table like, Table1(Contract_id, name, address, contact_no) and another table like, Table2(Contract_id,

  • 0

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;  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:34:51+00:00Added an answer on June 9, 2026 at 10:34 am

    Just to be clear the data is not duplicating. Since you are joining the two tables on contract_id and the same contract id is associated with two records, you will have the approver appear twice.

    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)
    

    So the result will always be two records from table1 and one record from table two with an approver = 'YZX'

    On another note, I might consider rewriting the query to the following:

    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'
    

    Based on your comment, I am beginning to think you might want a 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%'
    

    See SQL Fiddle with Demo

    Edit 2: There is not simple way to force this to use a null for additional fields. You can attempt to use a rownumber to get the results:

    select x.contract_id, 
      x.owner,
      x.address,
      case when (@rownum:=@rownum+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
    

    See SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like Table1: col1 col2 col3 and have an index over
I have a SQL Server 2008 DB with a table like this (Table1): ID
I have a table like this ID Name IsDeleted 1 Yogesh Null 2 Goldy
Background: I have a table with 5 million address entries which I'd like to
I have a Table like this one: |UserId | ContactID | ContactName --------------------------------------- |
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
I have a table like this : +-------+------------+------+-----+---------+-------+ | Field | Type | Null
I have a table like the following: RuleStep StepID | Step Property | Step
I have a table like this: ID country ------------- 1 US 2 Japan 3
I have a table like so; id1 id2 1 1 2 1 1 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.