58 users online (0 members and 58 guests)  


  Results 1 to 3 of 3

Related

  1. #1
    olaad's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2011
    Posts
    1

    How to write if statement in MySQL

    Hi everyone....
    I have a table in my database comprising of 'yesnoquestion_id', 'student_id', 'student_answer', 'answer'.
    I need to get some information out into my webpage interface using MySQL. I need to output the number of student whose answers answers are correct to the right answer into a table that will show on the interface..... the write answer is 'answer' while the students' answer is 'student_answer'.

    Here is what i have done so far:

    PHP Code:


    <?php

    global $connection;
    $query = "SELECT yesnoquestion_id AS Question_No, if (answer = 1, 'TRUE', 'FALSE') AS Answer, Count(student_id) AS num1
    FROM yesnoanswers
    WHERE student_answer = answer
    GROUP BY yesnoquestion_id
    ORDER BY yesnoquestion_id ASC ";
    $collate_set = mysql_query($query, $connection);

    //Drawing table and inserting data into it
    echo "<table border='5'>
    <tr>
    <th>Question Number</th>
    <th>Question Answer </th>
    <th>No. of Student correct</th>
    </tr>";
    while ($row = mysql_fetch_array($collate_set))
    {
    echo "<tr>";
    echo "<td>" . $row ['Question_No'] . "</td> ";
    echo "<td>" . $row ['Answer'] . "</td> ";
    echo "<td>" . $row ['num1'] . "</td> ";
    echo "</tr>";
    }
    echo "</table>";
    ?>

    This is outputting just the 'YESNOQUESTION_ID', 'NUMBER OF STUDENTS', 'STUDENT_ANSWER = ANSWER'.

    How can i get it to also output the 'STUDENT_ANSWER != ANSWER' i.e where 'student_answer' IS NOT 'answer' on the same table ?


    Thank you

  2. #2
    bparker1084's Avatar
    New User

    Status
    Offline
    Join Date
    Oct 2012
    Posts
    3

    Re: How to write if statement in MySQL

    For MySQL, an if statement can only be used in stored functions, not regular queries. There is an if function that can be used in queries that is similar to the ternary operator in other languages.

  3. #3
    carvermediagroup's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2013
    Posts
    2

    Re: How to write if statement in MySQL

    Currently my SQL looks like this:

    IF NOT (NEW.status <=> OLD.status) THEN
    {my sql}

    ELSEIF NOT (NEW.actual <=> OLD.actual) THEN
    {my sql}
    END IF