1

I have a table like:

id     name            children
1      Roberto         Michael,Dia
2      Maria           John,Alex
3      Mary            Alexandre,Diana

My problem is; I want to find who has a child named Alex.

I can't use "where children = 'Alex'" in SQL because I have more than one names in same cells.

So I use "where children LIKE '%Alex%'" - that looks smart but in the same time i get all start like Alex :( Alexandre or i want to get dia but result is dia and diana :(

how can I get single Alex in that data type?

I hope I can explain my problem with my terrible english :D

5
  • 2
    See normalization. It's pretty fundamental to relational databases. Commented Feb 20, 2015 at 23:01
  • 3
    Not a good idea to have a number of children under a table field. It would be better to create a second table where the names of the children are stored and join it to your main table by using a foreign key. Commented Feb 20, 2015 at 23:03
  • 2
    Strawberry and kidA are both saying the same thing: you need to break out "children" into a separate table, then link it back to "parents". The process is called "normalization". The "link" between parents and children is called a "foreign key". Commented Feb 20, 2015 at 23:07
  • 2
    And so is FoggyDay ;-) Commented Feb 20, 2015 at 23:09
  • Possible duplicate of Is storing a delimited list in a database column really that bad? Commented Aug 28, 2018 at 5:31

4 Answers 4

9

The best solution would be to normalize your schema. You should have a separate table with one row for each child, instead of a comma-delimited list. Then you can join with this table to find parent with a specific child. See @themite's answer for an example of this.

But if you can't do that for some reason, you can use FIND_IN_SET:

WHERE FIND_IN_SET('Alex', children)
0
1

You should split the data into two tables.

the first would look like this

ID    Name
1     Roberto
2     Maria
3     Mary

And the second like this

ParentId     child
1            Michael
1            Dia
2            John
2            Alex

and so on.

then you could do the query you want without having to worry about like and your data is much more useable

1

That's why you'd want to have two tables here.

parents:
id  name
1   Roberto  
2   Maria
3   Mary

children:
id  parentid name
1   1        Michael
2   1        Dia
3   2        John
4   2        Alex
5   3        Alexandre
6   3        Diana

And now you can query this much more effectively with a join or an exists:

SELECT *
FROM Parents
WHERE EXISTS(
    SELECT * 
    FROM Children 
    WHERE parentid=Parents.id 
      AND Children.name='Alex'
 )
1

I would rather make different tables for children and parents something like this.

Table for parents

parent_id     name            
1             Roberto         
2             Maria           
3             Mary     

Table for children

children_id  parent_id  name
1            1          Michael     
2            1          Dia 
3            2          John

.... and so on

2
  • I'm just making fun of your idiom :-) Commented Feb 20, 2015 at 23:17
  • Well I didn't want to get technical with him as it seem like he doesn't know much (based on his question). Commented Feb 20, 2015 at 23:18

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.