Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Why oh why SQL do you mock me?, please help

Options
  • 08-11-2010 12:17pm
    #1
    Registered Users Posts: 498 ✭✭


    Lets say I have two tables {A, B}
    A = {pk, fieldA, fieldB, etc}
    B = {pk, A.pk, field}
    
    The second column in table B being a foreign key to the primary key of table A.

    What I'm trying to get is a result set with the following

    A.pk, A.fieldA, (single random selection from B.field)

    MySQL DB btw.

    I really appreciate any help you can give me with this, as I am having a serious brain mush day.


Comments

  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    What, you mean like this?

    SELECT A.pk, A.fieldA, B.field from A LEFT JOIN B ON A.pk = B.A.pk


  • Registered Users Posts: 498 ✭✭bobbytables


    Problem sorted, thank you Sparks for your help.

    /scratches beard, now what the hell am I going to have for lunch?


  • Registered Users Posts: 14,339 ✭✭✭✭jimmycrackcorm


    Except you'll get multiple results.

    If you want a random result then you're going to make it more complicated by also including a rowId as the subquery and selecting the top 1 where rowId >= random( count of the result set)

    sigh....


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    I thought when he said single random selection from B's fields, he just meant any field rather than an actually random() selection...


Advertisement