Musikdoktor
Musikdoktor

Reputation: 144

2 mysql queries at once

I have a table like this one..

[id][Col1][Col2][id_duplicated]
 10  abc1  defg  NULL
 12  text  text  NULL
 50  abc2  text  NULL
 90  NULL  NULL  10    
 500 NULL  NULL  10 
 620 NULL  NULL  50
 700 text  text  NULL

Id_duplicated is a value that marks that is a copy from the row 'id' on the same table.. if i select id 620 will display all values from id 50

The problem:

select id,col1 from table where col1 is like '%abc%'

Only will show the row id=10 and id=50 but also i need to display if there's any copies of those id's

i imagine i need a subquery that first find if there's any '%abc%' and then a second one to check if there's any copy on id_duplicated equal to 10 and 50 and etc etc....

So in conclusion i need one query that display this result

[id][Col1][Col2][id_duplicated]
 10  abc1  defg  NULL
 50  abc2  text  NULL
 90  NULL  NULL  10    
 500 NULL  NULL  10 
 620 NULL  NULL  50 

Sorry my english

Upvotes: 1

Views: 46

Answers (1)

Philipp
Philipp

Reputation: 2796

If you want additional rows you can use union and a select looking for the id of the result into the id_duplicated:

http://sqlfiddle.com/#!9/ad817/8

select id,col1, col2, id_duplicated 
    from table 
    where col1 like '%abc%'
UNION 
select id, col1, col2, id_duplicated 
    FROM table 
    WHERE id_duplicated IN (select id from table where col1 like '%abc%')

Upvotes: 2

Related Questions