14

This is an excerpt from one table:

| id | type    | other_id | def_id | ref_def_id|
| 1  | int     | NULL     |  5     | NULL     |
| 2  | string  | NULL     |  5     | NULL     |
| 3  | int     | NULL     |  5     | NULL     |
| 20 | ref     | 3        |  NULL  | 5        |
| 21 | ref     | 4        |  NULL  | 5        | 
| 22 | ref     | 5        |  NULL  | 5        |  

What I want is to find entries with type ref. Then I would for example have this one entry in my result:

| 22 | ref     | 5        |  NULL  | 5        |  

The problem I am facing is that I now want to combine this entry with other entries of the same table where def_id = 5.

So I would get all entries with def_id = 5 for this specific ref type as result. I somehow need the output from my first query, check what the ref_def_id is and then make another query for this id.

I really have problems to understand how to proceed. Any input is much appreciated.

4 Answers 4

12

If I understand correctly you need to find rows with a type of 'ref' and then use the values in their ref_def_id columns to get the rows with the same values in def_id. In that case you need to use a subquery for getting the rows with 'ref' type and combine it using either IN or EXISTS:

select *
from YourTable
where def_id in (select ref_def_id from YourTable where type='ref');

select *
from YourTable
where exists (select * from YourTable yt
  where yt.ref_def_id=YourTable.def_id and yt.type='ref')

Both queries are equivalent, IN is easier to understand at first sight but EXISTS allow more complex conditions (for example you can use more than one column for combining with the subquery).

Edit: since you comment that you need also the id from the 'ref' rows then you need to use a subquery:

select source_id, YourTable.*
from YourTable
join (select id as source_id, ref_def_id
      from YourTable
      where type='ref')
as refs on refs.ref_def_id=YourTable.def_id
order by source_id, id;

With this for each 'ref' row you would get all the rows with the associated ref_id.

6
  • Thank you so much, this helped me a lot. Now I see another problem. Is it possible to keep track to which type the entries belong? For example if I want to see for id 20, type ref, other_id 3 all the relevant information. The query works perfectly, but I noticed, that I need to have a track. Now I get the query result with all refs and I dont see to which initial id they belong. Is my explanation clear? If you have any solution, thanks for helping out
    – curlie
    Commented Oct 28, 2017 at 18:25
  • I think I understand, you want the id of the row from where ref_def_id was obtained, but the problem is that with your sample data you would get a lot of combinations. I mean, as you have duplicate values in that columns you would get (duplicated ref_def_id count) x (duplicated def_id count) rows for each value of def_id. For example, for ref_def_id=5 you would get 9 result rows, that is, (3 rows with def_id=5) x (3 rows with ref_def_id=5). I this what you want? Commented Oct 28, 2017 at 18:47
  • Yes it goes into this direction. When I make a query for example for id 20, then I should get back 3 rows, since value is ref and the ref_def_id is 5 and there are 3 rows where def_id has the value 5. You can imagine like that ref_def_id is a reference to def_id and thus when I make a query where ref_def_id IS NOT NULL, there is always a reference to this def_id column. This is the problem I am having...
    – curlie
    Commented Oct 28, 2017 at 20:29
  • Thank you so much. This is exactly what I was looking for. It is very kind that you helped me out. May I ask you one last question? It is nice that the source_id is now in the list to have track. From my example above If I also wanted to keep the column other_id in the final list, how to extend your code example? I tried to do the same as you did with source_id, the new column then has only NULL entries.
    – curlie
    Commented Oct 29, 2017 at 9:09
  • Just put other_id in the subquery, for example other_id as source_other_id, if you are getting nulls either they are nulls on that column or you are using in the select the column from the main table instead of the column from the subquery (when in doubt execute only the subquery part to see what is going on). Commented Oct 29, 2017 at 11:39
2

use below query to get column from sub query.

select a.ref_def_id
from (select ref_def_id from YourTable where type='ref') as a;
0
0

What you are looking for is a subquery or even better a join operation.

Have a look here: http://www.mysqltutorial.org/mysql-left-join.aspx

Joins / the left join allows you to combine rows of tables within one query on a given condition. The condition could be id = 5 for your purpose.

0

You would seem to want aggregation:

select max(id) as id, type, max(other_id) as other_id,
       max(def_id) as def_id, ref_def_id
from t
where type = 'ref'
group by type, ref_def_id
2
  • Regarding the above, when I try to make this query I get the error response that there is an unkonwn column ref. Any idea?
    – curlie
    Commented Oct 28, 2017 at 17:46
  • @curlie . . . That should be type in the group by. Commented Oct 28, 2017 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.