Hey. I want to write 开发者_StackOverflow中文版a sql query for a table:
user_1(question_id int, option char(1));
Here, in this table, I am having question_id
arranged randomly and client will send the previous question_id
to the server. Then, I need the next question_id
in table coming after the question_id
sent by client.
I can do this with the server side scripting but is it possible without that? I mean directly having the next question_id
with sql.
Sample Data:
4, 'A'
2, 'B'
7, 'C'
You have no way of determining which question should go next. If you query a table without any order specified it might look like you have a set of rows ordered by time of insert. While this is true for small tables you should not rely on it. Any fair amount of record inserting/deleting or doing table analyze can change this order and you end up with your order being wrong. If you don't have any other column keeping order at which you inserted data there is completely no guarantee you will get it right after doing select without order by clause.
If I understand you correctly...
SELECT MIN(question_id) FROM user_1 WHERE question_id > (value client sent)
select id from question
where id not in (already asked)
order by rand()
limit 0,1
edit: Seeing updated question, the answer is - no way. You need some other field that will set the order of the questions, because there's no way to tell now what order they come in.
You should think about database table as about UNORDERED record set.
精彩评论