I'm working on a review system, where reviews for products are shown based on the outcome of a questionnaire.
Example:
What food do you like (Italian, French or fusion)
Which vegetables do you like (spinach, tomato or broccoli)
How much money would you like to spend on dinner ($0-$10, $11-$20 or $21-$30)
If someone selects Italian, tomato, $0-$10, then the user would see (for example) 20 types of pizza's, with reviews from other users shown.
But we only want to show products that have been reviewed for the exact same outcome of the questionnaire (we call this the protocol).
Table: questions
-----------------------------------
| id | question |
-----------------------------------
| 1 | What food do you like |
-----------------------------------
| 2 | Which vegetables do you... |
-----------------------------------
Table: possible_answers
-----------------------------------
| id | question_id | answer |
-----------------------------------
| 1 | 1 | Italian |
-----------------------------------
| 2 | 1 | French |
-----------------------------------
Table: products
---------------------
| id | product |
---------------------
| 1 | Pizza Salami |
---------------------
| 2 | Pizza cheese |
---------------------
From a database perspective (we're using mySQL), I'm wondering how to store the protocol attached to each review
[OPTION 1]
Table: reviews
---------------------------------------------------------------------------
| id | product_id | answer_q1_id | answer_q2_id | answer_q3_id | rating |
---------------------------------------------------------------------------
| 1 | 2 | 3 | 57 | 166 | 4 |
---------------------------------------------------------------------------
| 2 | 25 | 3 | 57 | 166 | 5 |
---------------------------------------------------------------------------
[/OPTION 1]
[OPTION 2]
Table: reviews
-------------------------------
| id | product_id | rating |
-------------------------------
| 1 | 2 | 4 |
-------------------------------
| 2 | 25 | 5 |
-------------------------------
Table: protocol
-----------------------------------------------
| id | review_id | question_id | answer_id |
-----------------------------------------------
| 1 | 1 | 1 | 3 |
-----------------------------------------------
| 2 | 1 | 2 | 57 |
-----------------------------------------------
| 2 | 1 | 3 | 166 |
-----------------------------------------------
[/OPTION 2]
Normally, I'd go with option 2 which just 'feels' better. But I'm not sure how one could query the protocol table to get all product reviews for a specific path in the questionnaire (protocol). For example when the user answers 'italian, spinach, 0-10', how do I find out which product reviews go with that combination of answers using option 2 (with option 1 it is obvious how to do this)?
I hope I've explained this well and I'm looking forward to reading thoughts from the community.
Greatly appreciated,
Lionel.
Related
I've got a procedural question that is likely going to expose how burnt out I am. I have 3 tables, users, projects and project_users. projects is joined with project_user to allow users to share projects.
I'd like to let, say, Bob see all of his projects and who he is sharing them with. In the example below Bob's sharing his 1 project with everyone. In his dashboard I'd like to show him "Bob's 1 Project" with everyone it's shared with. This is being managed in a while loop because Bob really has dozens of projects he's sharing with dozen's of people. The problem is, how can I show 1 project with everyone it's shared_with instead of showing the same project over and over each time there's a new shared_with value?
id | users | | id | projects | | id | owner_id | shared_with | project_id | role
---+-------------------- ---+------------------- ---+----------+-------------+------------+------
1 | bob#here.com | | 1 | Bob's 1 Project | | 1 | 1 | NULL | 1 | 1
2 | tom#there.com | | 2 | Tom's | | 2 | 1 | 4 | 1 | 2
3 | jan#red.com | | 3 | Jan's | | 3 | 1 | 2 | 1 | 2
4 | joe#somewhere.com | | 4 | Joe's Project | | 4 | 1 | 3 | 1 | 2
5 | fred#where.com | | 5 | Fred's Project | | 5 | 1 | 5 | 1 | 2
So I am building a webpage that shows a bunch of video games located in a SQL database and one suggestion I had was to have the different prices from each region display based on a drop down. My question is trying to figure out whats the best way to store int in the database. Would it be like:
GAME
CountryID
price1
CountryID
price2
CountryID
price3 ...
Or is there a better way to do this?
Just a heads up I've only been developing web applications for a year or so and I'm still pretty new to SQL.
Thanks for your input!
I would use multiple tables, one for games and one for region pricing.
Games
+--------+----------+
| GameID | GameName |
+--------+----------+
| 1 | Game1 |
| 2 | Game2 |
| 3 | Game3 |
| 4 | Game4 |
+--------+----------+
RegionPricing
+----------+--------+-------+
| RegionID | GameID | Price |
+----------+--------+-------+
| 1 | 1 | 60 |
| 1 | 2 | 55 |
| 1 | 3 | 45 |
| 1 | 4 | 80 |
| 2 | 1 | 50 |
| 3 | 2 | 30 |
| 3 | 3 | 25 |
| 3 | 4 | 45 |
| 4 | 1 | 60 |
| 4 | 2 | 55 |
| 4 | 3 | 45 |
| 4 | 4 | 80 |
+----------+--------+-------+
By using separate tables you minimize duplicate data and allow for easy granular changes. You may also consider adding a column to RegionPricing for currency. This would also need a Region table, with RegionID and RegionName.
I have a need to display user-levels for all possible users(T1) in all possible Pages(T2) whereby the values are stored in another table (T3) like:
UserID, Page, Level(R,E,W,A,O)
(Read,Edit,Write,Assistant,Owner)
But I just can't understand how to dynamically get the columns made and then refer to it as LAST(col1)
Any hint in the right direction will be appreciated.
I know how to get it done in a number of foreach loops in php but I believe it should be possible in MySql?..
Tables look like:
Table 1; Users
ID | Name
------------------
1 | James
------------------
2 | John
------------------
3 | Peter
------------------
Table 2; Pages
ID | PageName
------------------
1 | Home
------------------
2 | Members
------------------
3 | Courses
------------------
4 | Lybrary
------------------
5 | StrangePage
------------------
6 | Unused Page
------------------
Table 3; UserLevels
ID | User | Page | Lev
---------------------------------------
1 | James | Home | W
---------------------------------------
2 | James | Members | R
---------------------------------------
3 | James | Lybrary | O
---------------------------------------
4 | John | Home | R
---------------------------------------
5 | Peter | Home | O
---------------------------------------
6 | Peter | Courses | A
---------------------------------------
Expected out put would contain all users as rows, all pages as columns, all permissions (where available) as results:
User | Home | Members | Courses | Lybrary | StrangePage | Unused Page
-----------------------------------------------------------------------
James | W | R | | | |
-----------------------------------------------------------------------
John | R | | | | |
-----------------------------------------------------------------------
Peter | O | | A | | |
-----------------------------------------------------------------------
In a simple sales sample I have 3 primary tables: Order, Product, Orders_Products:
Product table
+----+-----------+
| id | name |
+----+-----------+
| 1 | A |
| 2 | B |
+----+-----------+
order table
+----+---------+------------+------------+---------------------+
| id | user_id | total_cost | order_date | status_id |
+----+---------+------------+------------+---------------------+
| 1 | 5 | 25.00 | 2012-02-03 23:30:24 | 1 |
| 2 | 7 | 30.00 | 2012-02-13 18:06:12 | 1 |
+----+---------+------------+------------+---------------------+
orders_products table
+----+----------+------------+--------+
| id | order_id | product_id | cost |
+----+----------+------------+--------+
| 1 | 1 | 34 | 10.00 |
| 2 | 1 | 25 | 10.00 |
| 3 | 1 | 27 | 2.50 |
| 4 | 1 | 36 | 2.50 |
| 5 | 2 | 75 | 25.00 |
| 6 | 2 | 74 | 5.00 |
+----+----------+------------+--------+
But in my system:
A user adds money into his account then he can spend it
Products are services like Product A: user can post 5 Ads so when user post a Ads that 5 become 4 and so on.
How should I design DB for it??
You currently have no table to store customer data. I would strongly advise creating one using an Auto-Increment ID field, a name field and a field to store the money on their account which can be modified when the add money to it or purchase something.
In the Order table, the user_id field should be a foreign key from the Customer table I just told you how to create.
If you want to better understand what you should be doing, I would recommend looking at the Entity-Relationship Model
I don't entirely understand what your second question is, can you explain it in more detail? Are all the products just a set number of ads that the customer can post?
I have three tables:
users:
+----------+-----------------------------+-----------+
| users_id | user detail | otherID |
+----------+-----------------------------+-----------+
| 1 | user name or details | 1 |
| 2 | user name or details | 1 |
| 3 | user name or details | 4 |
| 4 | user name or details | 1 |
| 5 | user name or details | 21 |
| 6 | user name or details | 2 |
+----------+-----------------------------+-----------+
photos:
+----------+----------------+-----------+--------+---------+--------------+
| photosID | url | title | userID | likes | remarksID |
+----------+----------------+-----------+--------+---------+--------------+
| 1 | 7459.JPG | TITLE | 1 | 150 | 255 |
| 2 | 7510.JPG | TITLE | 1 | 146 | 247 |
| 3 | 7460.JPG | TITLE | 2 | 2 | 56 |
+----------+----------------+-----------+--------+---------+--------------+
remarks:
+-----------+---------------------------------------+---------+------------+
| remarksID | remark | userID | photoID |
+-----------+---------------------------------------+---------+------------+
| 1 | REMARKS for PhotoID 1 | 1 | 1 |
| 2 | REMARKS for PhotoID 1 | 1 | 1 |
| 3 | REMARKS for PhotoID 1 | 1 | 1 |
| 4 | REMARKS for PhotoID 2 | 1 | 2 |
| 5 | REMARKS for PhotoID 2 | 1 | 2 |
| 6 | REMARKS for PhotoID 3 | 2 | 3 |
+-----------+---------------------------------------+---------+------------+
I am trying to extract the remarks for a given users photo, but only want the photo to appear once but all the relevant remarks for the photo to be displayed for each users photo.
If there are no remarks then just display the photo without remarks.
The layout would look like this on a web page-
------------------------------------
Photograph 1
Remarks 1
Remarks 2
Remarks 1
------------------------------------
Photograph 2
NO remarks for photo
------------------------------------
Photograph 3
Remarks 1
Remarks 2
Remarks 3
Remarks 4
------------------------------------
Photograph 4
Remarks 1
Remarks 2
------------------------------------
Is my assumption correct that your difficulty is, how to get the database to do this? If so, then the answer is that this function (formatting the data), is not (or should not) generally be the responsibility of the database. Fetch the photos from the database as one result set, then fetch the matching comments in another, and let your application or reporting tool format the data the way you want it.