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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

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

  • 08-11-2010 11:17am
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 14,403 ✭✭✭✭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, Registered Users 2 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