Suppose I have a experts table like this :
+-----------+----------+----------+--------+
| expert_id | name | family | active |
+-----------+----------+----------+--------+
| 1 | ali | ahadi | 1 |
| 2 | reza | hosseini | 1 |
| 3 | hossein | rezaei | 0 |
| 4 | mohammad | gholami | 1 |
+-----------+----------+----------+--------+
And in the other hand there is a questions table like this :
+-------------+-------+-----------+
| question_id | title | expert_id |
+-------------+-------+-----------+
| 1 | A | 1 |
| 2 | B | 2 |
| 3 | C | 4 |
| 4 | D | 1 |
| 5 | E | 2 |
| 6 | F | 4 |
+-------------+-------+-----------+
I'm working on a Question-Answer App. when a user asks a new Question I want to select an expert(of course activated expert, means an expert that has active field equal to 1) that can answer to question .(expert_id field holds the selected Expert ID in the questions table).
But I do not want this to be a random selection. Instead, I want to be sequentially as you can see in the expert_id of questions table.
In fact ,since that daily many questions may be asked I would like divide questions between the experts equally.
I want to do it in laravel But I do not know how I could implement this logic.
Can anyone help me to do that or suggest better ways?
I would suggest you keep a running total of the number of questions each expert has, that way you can assign it to the person with the lowest number of questions. You could even make this the total number of unanswered questions each expert has, which gets lowered when an expert answers the question so those experts that answer questions more frequently get more questions and those with less time to answer questions, or difficult questions that take a long time to answer, get less questions.
The way to do this would be to add another field onto the experts table, num_questions. When selecting experts you could do something like $expert = Expert::where('active', '=', 1)->orderBy('num_questions')->first(); then just assign the question to that user and increment the num_questions field by one for that user. You'd then just need to decrement that number when the user answers a question (if you want unanswered questions over total questions).
Related
I have 3 tables:
1- questions
2- answers
3- user_answers
questions
_________________
| id | question |
|____|__________|
| 1 | 1+1 |
|____|__________|
answers
_________________
| id | answer |
|____|__________|
| 1 | 1 |
|____|__________|
| 2 | 3 |
|____|__________|
| 3 | 2 |
|____|__________|
user_answers
______________________________________________
| id | answer_id | question_id | user_id |
|____|_____________|______________|___________|
| 1 | 1 | 1 | 1 |
|____|_____________|______________|___________|
| 2 | 3 | 2 | 1 |
|____|_____________|______________|___________|
| 3 | 2 | 3 | 1 |
|____|_____________|______________|___________|
So as you see there is many to many relation within user_answers table.
Instead of saving each question with its answer in a row, I want to save all questions and answers as an array. So there will be 1 row for each user for performance.
I searched about that and found serialize() function and that the datatype for that column should be Blob.
So the new table would be like this:
user_answers
______________________________
| id | array | user_id |
|______|_________|___________|
| 1 | Blob | 1 |
|______|_________|___________|
The array could be like that:
$array = array(
1 => 1,
2 => 3,
3 => 2
);
There could be like 20s or more questions.
Is that's the best way for doing this? or saving each question in a row? or using another datatype for the array column?
1 row for each user for performance
That may backfire.
If you pack a bunch of stuff together via serialization, JSON, etc, you need to avoid looking into the stuff. That is, the only practical and efficient thing to do is fetch the entire string into your application and break it apart there.
If you might need to, say, fetch "what every user gave as an answer just for question 3, then the question number needs to be its own column.
For an online game, I have a table that contains all the plays, and some information on those plays, like the difficulty setting etc.:
+---------+---------+------------+------------+
| play-id | user-id | difficulty | timestamp |
+---------+---------+------------+------------+
| 1 | abc | easy | 1335939007 |
| 2 | def | medium | 1354833214 |
| 3 | abc | easy | 1354833875 |
| 4 | abc | medium | 1354833937 |
+---------+---------+------------+------------+
In another table, after the game has finished, I store some stats related to that specific game, like the score etc:
+---------+----------------+--------+
| play-id | type | value |
+---------+----------------+--------+
| 1 | score | 201487 |
| 1 | enemies_killed | 17 |
| 1 | gems_found | 4 |
| 2 | score | 110248 |
| 2 | enemies_killed | 12 |
| 2 | gems_found | 7 |
+---------+----------------+--------+
Now, I want to make a distribution graph so users can see in what score percentile they are. So I basically want the boundaries of the percentiles.
If it would be on a score level, I could rank the scores and start from there, but it needs to be on a highscore level. So mathematically, I would need to sort all the highscores of users, and then find the percentiles.
I'm in doubt what's the best approach here.
On one hand, constructing an array that holds all the highscores seems like a performance heavy thing to do, because it needs to cycle through both tables and match the scores and the users (the first table holds around 10M rows).
On the other hand, making a separate table with the highscore of users would make things easier, but it feels like it's against the rules of avoiding data redundancy.
Another approach that came to mind was doing the performance heavy thing once a week and keep the result in a separate table, or doing the performance heavy stuff on only a (statistically relevant) subset of the data.
Or maybe I'm completely missing the point here and should use a completely different database setup?
What's the best practice here?
I've read a lot of pages but didn't find what I was looking for.
I am trying to figure out which is the most appropriate solution for my problem here. I currently got a multiple choice form with 23 questions and 23 comments section (650 characters each, basically a comment per question) and I am not sure if I should go with a single table or if that would be too much.
So basically something like:
id
user_id
date
multiplechoice1
..
multiplechoice23
comment1
..
comment23
status
That will be around 50 columns :/ Is there a better way to do this? Like split the comments on a different table or somehow combine all the multiple choices in a single column? Since each answer will be 1-5.
Example of the question:
How long do you usually surf the NET on a daily basis?
0-1 hour
2-3 hours
4-6 hours
more than 7 hours
Taking your comments into consideration, being that you don't want to normalize too much and prefer something slightly flattened, I think this may be a proper solution for you.
questions
+----+-------------------------------+
| id | question |
+----+-------------------------------+
| 1 | What is your favorite food? |
+----+-------------------------------+
| 2 | What is your favorite animal? |
+----+-------------------------------+
answers - question 1 has three answers, question 2 has two answers
+----+-------------+-----------+
| id | question_id | answer |
+----+-------------+-----------+
| 1 | 1 | Spaghetti |
+----+-------------+-----------+
| 2 | 1 | Chicken |
+----+-------------+-----------+
| 3 | 1 | Sushi |
+----+-------------+-----------+
| 4 | 2 | Dog |
+----+-------------+-----------+
| 5 | 2 | Cat |
+----+-------------+-----------+
answer_data - one person answered "spaghetti" for question 1, one person answered "cat" for question 2, and one person answered "dog" for question 2
+----+-------------+-----------+
| id | question_id | answer_id |
+----+-------------+-----------+
| 1 | 1 | 1 |
+----+-------------+-----------+
| 2 | 2 | 4 |
+----+-------------+-----------+
| 3 | 2 | 5 |
+----+-------------+-----------+
Truthfully I'm not great with MySQL, I'm a T-SQL guy, but the query would probably look something like this:
SELECT q.question, a.answer, COUNT(ad.answer_id) as `Count`
FROM questions q
JOIN answers a ON a.question_id = q.question_id
JOIN answer_data ad ON ad.question_id = q.question_id
GROUP BY q.question, a.answer
+-------------------------------+-----------+-------+
| question | answer | count |
+-------------------------------+-----------+-------+
| What is your favorite food? | Spaghetti | 1 |
+-------------------------------+-----------+-------+
| What is your favorite animal? | Dog | 1 |
+-------------------------------+-----------+-------+
| What is your favorite animal? | Cat | 1 |
+-------------------------------+-----------+-------+
Scenario:
I'm trying to create a interviewer robot. The robot asks multiple questions from everyone and then gives him/her a result. Overall, that robot investigates a person by asking some questions and then approves or rejects him/her for being employ in a company. Something like this:
Image above is just a simplified of real questions. In reality there is almost 1000 questions which everyone has to answer almost 40 questions (based on his/her previous answer and company's needed).
My Question:
I have a table which contains those questions. Something like this:
// questions
+----+----------------------------+
| id | question |
+----+----------------------------+
| 1 | What's your gender? |
| 2 | What is your height? |
| 3 | What's your age? |
| . | . |
| . | . |
| . | . |
+----+----------------------------+
Ok well, how can I find next question for each person based on his previous answers? I mean what columns should I add to database to use them as a navigator?
| Level | id | question | answer
----------------------------------
| 1 | 1 |Your age | 18-50
| 2 | 2 |Your height| 150-175-190
| 3 | 3 | wight | 60 -120
| 4 | 4 | skill | skill set
get level 1
Your age ? --
chcek age in answer -- fails-> exit / pass-> level = level+1
level 2
yor heght
check height in (150-175) -- pass = level = level + 2
check height in (175-190)-- pass = level = level+1
fails - exit
next_level
Actually this question is completely choice based and can implement as per his/her own choice of methods to achieve the result .. One of approach can be like the above stated
Can you be more specific please?
For example, let's suppose a man already answered 8 questions and the current question is "do you like pets?" If yes the next question will be "do you like coffee?" else (if not) the next question will be "are you married?"
now let's suppose that a girl already answered 5 a questions and the current question is "do you like pets?"
Does the next potential questions will be the same than for the man??
So does the full path of answers determine the next question or does only the last precedent answer determine the next question??
I am little confusing for building schema of quiz
In this I have to upload many questions and having four options each option contains textbox and corresponding checkbox that denotes for right answer. if admin select one checkbox that could be right answer.
Note:- In some cases I have uploaded many option 6 to 7 and answers might be 2 or 3 are correct and admin will click on many checkboxes
Can anyone helping in schema
This seems fairly straight forward. You just have three tables, one for quizzes, one for questions and one for answers. Something like this:
Quizzes
+----+-------------+-----------------+
| id | name | description |
+----+-------------+-----------------+
| 1 | Sample Quiz | An example quiz |
+----+-------------+-----------------+
Questions
+----+---------+------------+
| id | quiz_id | question |
+----+---------+------------+
| 1 | 1 | Question 1 |
+----+---------+------------+
Answers
+----+-------------+----------+------------+
| id | question_id | answer | is_correct |
+----+-------------+----------+------------+
| 1 | 1 | Answer 1 | 0 |
| 2 | 1 | Answer 2 | 1 |
| 3 | 1 | Answer 3 | 0 |
| 4 | 1 | Answer 4 | 0 |
+----+-------------+----------+------------+
This schema will support as many quizzes as you need, each with any number of questions and each question can have any number of answers.