Monday, 8 May 2017

Express js:Many to many relationship mysql get all art-image under a collectio

I've four tables in my DB

1.arts table (id,image_name)
2.users table (id,username)
3.collections table(id,user_id(FK users),collection_name)
4.art_collections(id,art_id(FK arts),collection_id(FK collections))

user can create multiple collections it is stored in collections table

user can add multiple arts to a collection (stored in art_collections table).

Sample input

arts               users         collections                  

id image_name     id username     id user_id  colletion_name

1  test1.jpg      1  foo          1     1     first-collection
2  test2.jpg      2  bar          2     1     second-collection





   art_collections

   id art_id collection_id
   1   1      1
   2   2      1
   3   1      2

As you see first-collection have two arts and second-collections have one art now i want to query all collections with art image_name with a condition. if collections contains more than three art the image_name limited to three

Here is my expected result as array

[
{
 id:1,
 user_id:1,
 collection_name:first-collection
 image:["test1.jpg","test2.jpg"]
},
{
 id:2
 user_id:1
 collection_name:"Second-collection"
 image:["test2.jpg"]
}
]

Note: I want MYSQL query for that the above JSON is for understanding my expected output



via Jabaa

No comments:

Post a Comment