开发者

when there are 2 array results in sql and each result containing 5 answers..then how to compare the 2 arrays element by element.?

here "user" is a table name and a_i are the answers to the 5 questions answered in the quiz... now if i want to compare these two results like comparing the ans1 of the user 1 and user 2 what is the code i need to write... basically what i am thinking of is i will store all the coorrect answers in the users table under the user_id=1 and when other users play the quiz i will manage to store the results in the same table.. whi开发者_运维百科le evaluating i will compare the every user with user_id=1.... so now how to compare....

$sql="select a_1,a_2,a_3,a_4,a_5 from users where user_id=1"; $result1=mysql_query($sql);

$sql="select a_1,a_2,a_3,a_4,a_5 from users where user_id=2"; $result2=mysql_query($sql);


Do a self join on the user table and use aliases like so

    FROM USER answer, USER contestant
   WHERE answer.id = 1
     and contestant.id = 2

Then use the If function in your select clause to output correctness such as

SELECT IF(answer.a_1 = contestant.a_1, 'Yes', 'No') as A_1_Successful

This should allow you to reduce your sql calls.


You're almost there. You can now do this:

compare($result1[a_1], $result2[a_1]);

The compare function can be done any way you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜