Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • SQL: How to display all the records related to common id

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 289
    Comment on it

    I was stuck in the following issue. Issue was related with retrieving common records in group. I have to display records which are common with each id.

    Following records are inserted into the table "mytable":

        id |         title
    -------+--------------------
        16 | Apple
        16 | Ball
        16 | Cat
        16 | Dog
        18 | Eagle
        18 | Fish
        20 | God
        20 | House
        41 | India
        41 | Jug
        41 | Kite

     

    I want the Output should be shown like this below:

        id |         title
    -------+------------------
        16 | Apple
           | Ball
           | Cat
           | Dog
        18 | Eagle
           | Fish
        20 | God
           | House
        41 | India
           | Jug
           | Kite

    This issue is resolved now and solution to this problem is as follows:

    Just write the query:

    SELECT id,GROUP_CONCAT(title separator '\n') FROM mytable GROUP BY id

    All done...

    Thanks for reading this post.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: