Jump to content

Recommended Posts

I have two tables full of data, one of the has a field corresponding to the primary keys of rows of the other table, in some cases there are more than one row they correspond to, so is there an easy way to add a list of corresponding values, or would I have to find a way to do that on my own, eg. using a string and making it comma delimited and than parsing it in php?

Link to comment
https://forums.phpfreaks.com/topic/168273-solved-list-of-data-easy-method/
Share on other sites

Hi

 

The function you need is group_concat.

 

For a rough example if you had a table of employees and a table containing their addresses with one row per address line order by line number, something like:-

 

SELECT Name, group_concat(AddressLine ORDER BY LineNo SEPARATOR ',')

FROM Employees

LEFT OUTER JOIN EmployeerAddress ON Employees.Id = EmployeerAddress.EmployeeId

GROUP BY Name

 

(of course grouping by name is a bit silly as there could potentially be duplicate names, but this is just to give you the idea).

 

All the best

 

Keith

I'll try to elaborate, hopefully, that will help.

 

I have table_thing consisting of:

id, title, description

and table_children consisting of:

id, title, belongs

 

'belongs' in table_children corresponds to the id's of things in table_thing.

So an example of the tables would be like:

 

table_thing

1JimA person

2SamAnother person

3Soupa food

 

table_children

1people1,2

2stuff that starts with s2,3

 

And I want to be able to search through these using like SELECT * FROM table_children WHERE belongs=2, showing children that belong to item 2

Hi

 

Not really possible. There are bodge ways around it but you would land up with hideously unreadable and complicated code.

 

The real solution is to split off the belong column onto a seperate table, with one row per item per child:-

 

table_belongs

id child_id thing_id

1 1 1

2 1 2

3 2 2

4 2 3

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.