Select problem Mysql

Discussion in 'MySQL' started by cigi, Jul 8, 2009.

  1. #1
    Hi guys,


    I have average PHP skills (and still learning) but very low Mysql skills. And really need help with this new project I've taken on.

    What I need to do is select from a table that has 50 (50 rows) questions in.

    This selection needs to be random which I can do using ORDER BY RAND() LIMIT $i.

    However I also need to select 'mandatory' (all mandatory questions need to be displayed) questions from the same table, these are marked as mandatory within the table.

    So in a nut shell I need to select and display all of the mandatory question and then randomly select from the rest of the questions, but not displaying all of them (LIMIT maybe) and no repeat questions to display.

    At the moment I've tried:

    SELECT * FROM `fuel_1` WHERE man='1'
    UNION ALL 
    SELECT * FROM `fuel_1` WHERE man='0' ORDER BY RAND() LIMIT 6
    Code (markup):
    But this just displays 6 random questions, not all of the manatory questions and then a set amount of non manatory questions.


    Many thanks for your help in advance
     
    cigi, Jul 8, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Maybe something like this:

    SELECT * FROM `fuel_1` WHERE man='1'
    OR id IN(
    SELECT id FROM `fuel_1` WHERE man='0' ORDER BY RAND() LIMIT 6
    )
     
    jestep, Jul 8, 2009 IP
  3. cigi

    cigi Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply jestep

    As it happends the below code did work, I was adding it to a variable wrong :eek:

    SELECT * FROM `fuel_1` WHERE man='1'
    UNION ALL 
    SELECT * FROM `fuel_1` WHERE man='0' ORDER BY RAND() LIMIT 6
    Code (markup):
    Many thanks
     
    cigi, Jul 8, 2009 IP
  4. nirajkum

    nirajkum Active Member

    Messages:
    815
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #4
    your idea seems to be good ... to display random and mandatory question
     
    nirajkum, Jul 9, 2009 IP