mardi 16 juin 2015

Mysql query from two tables as parent and child objects

I am creating an Angularjs app with two tables "Contact Group" and "Contact List". The contact list table items have corespondent id to items in the contact group. What I want to achieve is a MySQL/Sqlite select query that will have each contact list items as a child object of its parent.

contact_group
  id | title 
 ------------
  1  | friends
  2  | mates
  3  | family

contact_list
 id  | gID | name | number
--------------------------
  1  |  1  | dave |0208500
  2  |  1  | dan  |4213839
  3  |  1  | sans |5656434
  4  |  2  | fola |1918982
  5  |  3  | brian|2398343
  6  |  5  | grace|0934232

Select query results examples (this is what i want to get)

id: 1
title: friends
child: {id:1, name:dave, number:0208500}, {id:2, name:dan, number:4213839},{id:3, name:sans, number:5656434}

I case there is another way of doing it, this what i want to achieve. I have created an accordion with title from the contact_group title. Under each accordion are the correspondent contacts from the contact_list.

I will be glad if anyone can help me. Thank you

Aucun commentaire:

Enregistrer un commentaire