0

I am working on a Query, but cant get it do what i want.

I have this table: family

family.id
family.name
family.related

With this content:

id        | name        | related
1         | Name1       | 
2         | Name2       | 3,5
3         | Name3       | 
4         | Name4       | 
5         | Name5       | 4,1

As you can see id 2 have related (ids)3,5 but if you go to (id)5 you get another related (ids)4,1

So when i select family.id 2 i want to output: 3,5,4,1

Is this possible with 1 query?

The following query brought me to 3,5 :

SELECT

GROUP_CONCAT(related1.related) AS related_ids,

family.id AS id,
family.name AS name,
family.related AS related

FROM family family

LEFT JOIN family related1
              ON FIND_IN_SET(family.id, related1.related) >0

WHERE family.id=5

I want to get related_ids to contain 3,5,4,1

9
  • Adding in your case if if 4 is related to lets say 6,7 then we have to display those also?
    – Meherzad
    Apr 9, 2013 at 12:53
  • yes that would be the case, just dont want to build up / copy the related id's to every row, i hope one id, can seach for its id in other family.related rows, and take their family.related ids as well, and keep building up a new column called related_ids.
    – WHOMEZz
    Apr 9, 2013 at 13:06
  • If you know number of level nesting has to be done then you can apply that many joins and get the desired result else it is not possible in single query. Also storing , separated values in tables is not a good practice.
    – Meherzad
    Apr 9, 2013 at 13:28
  • You can get desired result using a stored procedure or a multiple query.
    – Meherzad
    Apr 9, 2013 at 13:39
  • Please tell me how, im still trying my very best to get something i want... sqlfiddle.com/#!2/66306/17/0 it should be possible... the data is there, it just meed to repeat a inner query if there are related ids - just dont know the way to do it right
    – WHOMEZz
    Apr 9, 2013 at 14:04

0

Your Answer

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

Browse other questions tagged or ask your own question.