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 |
+-------------------------------+-----------+-------+
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.
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).
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.
I have already asked here Query Logic SQL and didn't get a response(could be with the presentation of my data) I really hope someone can take a look at this and provide their input on how to get this done. would highly appreciate any help.
I have a sql table data that looks like
users table
id | name |
_______________
1 | John |
2 | Mary |
3 | Charles |
4 | Mike |
5 | Lucy |
6 | Debbie |
pairing table:
main_id | pair_id |
_____________________
1 | 2 |
1 | 3 |
2 | 4 |
2 | 5 |
3 | 6 |
3 | 1 |
when rendering output to user, my html table would look like this, using group_by groupconcat in sql.
main_name | paired_names
John | Mary, Charles
Mary | Mike, Lucy
Charles | Debbie, John
Now,the problem is during searching(wildcard search)
say the user will input "Charles"...
The output needs to be:
main_name | paired_names
John | Mary, Charles
Charles | Debbie, John
since its going to do a wildcard search in both columns in the pair table.
For now, what i do is i manipulate the result set from the database but this has pagination and been advised that it will affect system performance if i query all data then manipulate after.
I hope someone will be kind enough to provide their advice on how to get this done.
I can provide further details if needed.
Looking forward to hear from you.
Checks the fiddle
SELECT users.id,GROUP_CONCAT(pair_id) FROM (SELECT users.id,
users.name,pairing.main_id,pairing.pair_id
FROM users, pairing
WHERE pairing.main_id=users.id
) AS t1 JOIN users ON users.id=t1.id GROUP BY users.id;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have been working with MySQL for a while now, and just recently found the need to manage my data better (MOAR DATA!)...
The problem I am having is this:
Table1: users
- id
Table2: companies
- companyid
- companyname
Table3: customers
- customerid
- companyid
I am trying to query the following.
I have the users ID, I need to use that to get the companyid from customers using the customerid, and return companyname based of the assigned companyid in customers.
It is very possible I am going about this very wrong. I understand that eventually the data is going to get very hard to read by eye as the data starts to grow. My concern is having the ability to associate and disassociate customers from businesses.
If you have any tips, or have a better strategy, or think I should just add this information into the users tables please let me know.
First off, you need some understandings on what Normalization is: http://support.microsoft.com/kb/283878
The database tables are not meant to be "read by eye". I'm pretty sure you are dealing with a very small database now, but imagine in the future you're dealing with thousands of tables with millions of rows, "visual inspection" is not going to work anymore.
A simple join would have given what you need:
SELECT t2.companyname
FROM table1 t1, table2 t2, table3 t3
WHERE t1.id = t3.customerid
AND t3.companyid = t2.company id
AND t3.customerid = (some id) //Depends on what your purpose is,
//this line can also be replaced by
//AND t1.id = (some id)
In your case, it is possible to combine User table and Customer table into one ONLY if all users are customers too. But it is definitely a NO to have company information in either User or Customer tables.
Assuming customers.customerid and users.id will be the same value, this should suffice:
SELECT companies.companyname
FROM customers
LEFT JOIN companies ON customers.companyid = companies.companyid
WHERE customers.customerid = 5
Here is a fiddle
Schema is on the left, sql is on the right.
Tables:
users
+--------+
| ID |
+--------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
+--------+
companies
+------------------------------------------+
| COMPANYID COMPANYNAME |
+------------------------------------------+
| 5 CompAlumpany |
| 9 Dergy Hergins LLC |
| 3 Smergy Berg Inc. |
| 23 Hergin Derz |
| 7 Comperation corpany |
| 11 Contagion Engine |
| 31 AEther Vial |
| 66 Necropotence |
| 90 Lord of Atlantis |
| 65 Snoogins |
| 51 Nickty-Schnickty-Schnoine |
| 58 Take a knee |
| 59 Coorprate |
+------------------------------------------+
customers
+--------------------------+
| CUSTOMERID COMPANYID |
+--------------------------+
| 1 5 |
| 2 9 |
| 3 3 |
| 4 23 |
| 5 7 |
| 6 11 |
| 7 31 |
| 8 66 |
| 9 90 |
| 10 65 |
| 11 51 |
| 12 58 |
| 13 59 |
+--------------------------+
Query Returns:
+---------------------+
| COMPANYNAME |
+---------------------+
| Comperation corpany |
+---------------------+
Posted on behalf of the OP.
I got the results I was looking for with the following:
SELECT t2.companyname FROM companies t2,customers t3 WHERE t3.companyid = t2.companyid AND t3.customerid=?
I was unaware I could create references, this will become very useful for me. Thank you!