Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Sql Help!!!

  • 12-02-2007 02:14PM
    #1
    Closed Accounts Posts: 230 ✭✭


    Been a while since I put a query together but I'm stuck getting my head around something. I want to run a query that using multiple wheres and returns two random examples of each. So far its just return 100 random examples from anywhere i.e. set in the fetch statement. Even if i could get it to return 1 of each I could work with that. Any help appreciated! :confused:

    SELECT PRODUCT, CONTRACT, NUMBER
    FROM (PRODUCT, CONTRACT, NUMBER, RAND() FROM db2) Q
    (PRODUCT, CONTRACT, NUMBER, RAND_NO)
    Where PRODUCT = 'M2'
    or PRODUCT = 'MT'
    or PRODUCT = 'CM'
    etc....
    ORDER BY RAND_NO
    FETCH FIRST 100 ROWS ONLY


Comments

  • Registered Users, Registered Users 2 Posts: 41 keeffe2001


    you could do this by merging a number of queries together and limiting each one to 2. Depending on which database you are using there is different key words to limit results (doing a search on google will clear it up)

    SELECT PRODUCT, CONTRACT, NUMBER
    FROM (PRODUCT, CONTRACT, NUMBER, RAND() FROM db2) Q
    (PRODUCT, CONTRACT, NUMBER, RAND_NO)
    Where PRODUCT = 'M2'
    ORDER BY RAND_NO
    LIMIT 0,2
    union

    SELECT PRODUCT, CONTRACT, NUMBER
    FROM (PRODUCT, CONTRACT, NUMBER, RAND() FROM db2) Q
    (PRODUCT, CONTRACT, NUMBER, RAND_NO)
    Where PRODUCT = 'MT'
    ORDER BY RAND_NO
    LIMIT 0,2
    union

    SELECT PRODUCT, CONTRACT, NUMBER
    FROM (PRODUCT, CONTRACT, NUMBER, RAND() FROM db2) Q
    (PRODUCT, CONTRACT, NUMBER, RAND_NO)
    Where PRODUCT = 'CM'
    ORDER BY RAND_NO
    LIMIT 0,2
    etc


  • Closed Accounts Posts: 230 ✭✭danindublin


    Thanks

    Seem to be a bit further along but its still looking for a fetch statment and doesnt like any variation of what was previously there! :mad:


Advertisement