database structure for multiple options on products - mysql

I have products in my database (MySQL).
I have my prices based on different combinations of options for my products.
I have the following data:
PRODUCTS
prodID | prodName
1 | Ball
OPTIONS
optionID | optionName | prodID
1 | color | 1
2 | size | 1
3 | material | 1
OPTIONVALUES
ovID | optionID | ovValue
1 | 1 | red
2 | 1 | blue
3 | 2 | small
4 | 2 | big
5 | 3 | wood
6 | 3 | glass
for prices I would like to have like this:
red small wood ball - $13
red small glass ball - 14
red big wood ball - 16
red big glass ball - 17
blue small wood ball - 12.5
[...]
How can I design the database structure for this?
I have tried several ways but none was good. if the number of the options were static then every option could have its own table, but this was that is not a good solution.

I like what you already have. If you want it to be fully flexible as i guess you do, i would do the following:
Add two more Tables.
Product_Instance
instanceID | prodID | price
1 | 1 | 13
Product_Instance_Options
instanceID | ovID
1 | 1
1 | 3
1 | 6
In this way you could define all possible combinations and prices for them. Might be a hell of a lot work to set up all the prices, but if you cant calculate them (like glass = +2$, small=2$ / big=4$) you will have this however you do it.

How about something like this:
productTypes
prodID | prodName
1 | Ball
2 | Car
productInstances
prodInstanceID | prodID | Prize
1 | 1 | $19
2 | 1 | $50
3 | 2 | $8
properties
propertyID | propertyName
1 | color
2 | size
3 | material
propertiesValues
pvID |propertyID | propertyName
1 | 1 | red
2 | 1 | blue
3 | 2 | small
4 | 2 | medium
5 | 2 | large
6 | 3 | wood
7 | 3 | glass
productProperties
ppID | prodInstanceID | pvID
1 | 1 | 1
2 | 1 | 3
3 | 1 | 6
4 | 2 | 2
5 | 2 | 4
6 | 2 | 7
7 | 3 | 1
8 | 3 | 3
9 | 3 | 6
In your design you can't determine which option belongs to which product instance. If you look at your table optionvalues, there is no way to group together your options.

Related

Does Cross Join not work between two different tables with same column name?

As written on the title, does CROSS JOIN not work for different tables with the same column name?
For example,
I have one table named Fruits:
| name | price |
| apple | 1 |
| banana | 2 |
and another table named Snacks:
| name | price |
| chips | 3 |
| cookies | 4 |
Then does
SELECT Fruits.price, Snacks.price FROM Fruits CROSS JOIN Snacks
does not work properly?
I am working on a same issue, but the result shows like:
| price | price |
| 3 | 3 |
| 4 | 4 |
| 3 | 3 |
| 4 | 4 |
But what I expect is:
| price | price |
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
As I mentioned in the comment, it is not possible. Either your tables values are different or your query.
Check this dbfiddle showing the result value same as your expected values.
In MySQL CROSS JOIN works as expected:
price price
------ -----
1 3
2 3
1 4
2 4
See running example at DB Fiddle.

SQL query select from multiple tables problem [duplicate]

This question already has answers here:
Many-to-many relationships examples
(5 answers)
Closed 1 year ago.
I have a mySQL database containing where Schools located in different countries provide training (the product) in different cars, with different objectives.
SCHOOLS
id| schoolname |
----------------------------------
1 | Pete’s Driving school |
2 | Karen’s Driving school |
3 | John’s Driving school |
4 | Donald’s Driving school |
CARS
id| carbrand |
----------------------------------
1 | Buick |
2 | Mercedes |
3 | Tesla |
4 | Audi |
PROVIDES (PROVIDES THIS TRAINING)
id| name |
----------------------------------
1 | Get License |
2 | Practise driving |
3 | Maneuvering |
4 | Winter training |
LOCATIONS
id| name |
----------------------------------
1 | USA |
2 | UK |
3 | France |
4 | Japan |
I also have these tables telling me which schools have which cars, which location, and provide which training:
SCHOOL_OWNS
id| schoolid | carsid
----------------------------------
1 | 1 | 2
2 | 1 | 1
3 | 2 | 1
4 | 3 | 2
SCHOOL_PROVIDES
id| schoolid | providesid
----------------------------------
1 | 1 | 2
2 | 1 | 3
3 | 2 | 1
4 | 3 | 2
SCHOOL_LOCATIONS
id| schoolid | locationsid
----------------------------------
1 | 1 | 2
2 | 2 | 4
3 | 2 | 1
4 | 3 | 2
My products which I'm trying to select and output..
PRODUCT
id| productname | schoolid | carid | locationid | price
----------------------------------
1 | Product1 | 2 | 1 | 4 | 400 USD
2 | Product2 | 3 | 1 | 2 | 300 USD
3 | Product3 | 1 | 2 | 1 | 200 USD
4 | Product4 | 2 | 2 | 1 | 100 USD
My question is now how to write a JOIN query to get what I'm looking for. This is doing my head in and not even sure where to start or if the way I have done it is a recommended way of setting it up?
I’m trying to create a SQL query that lets me select and sort depending on three criteria, car brand, provides and location.
I have on the top of the page three dropdown menus "CARS", "PROVIDES SERVICE" and "LOCATION".
What SQL Query could match these two scenarios?! Thanks I'm a bit lost..
1. If I want to display all products that fulfill all
these three criteria (Buick, Maneuvering, Japan)
cars=1&provides=3&location=4
Output (only one product meet this criteria):
Product1 | Karen’s Driving school | 400 USD
-------------------
2. And if I only want display all products that provide ‘practise driving’ ?cars=&provides=2&location= ,
Then all products should show that have a company that provides “practice driving” ..
Output (only one two products meet this criteria):
Product1 | Karen’s Driving school | 400 USD
Product3 | John’s Driving school | 200 USD
it looks like you can't make those joins since you do not have foreign keys in your tables.
as an example.. linking cars id to a provides id will not work, so it can't reach that table an easy way... you will need a foreign key or make a sub table to make it easier for yourself!

Whats the best way to store different prices for one entity in SQL?

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.

Need the difference between two columns ' different in number of items ' in two different tables in MS Access

I need the difference between items i bought and items i sold, items i bought in this table
ID |ItemName| PriceOfUnit | NumberOfItems I bought |DateIBought|
1 | tea | 3 | 6 |15/11/2015 |
2 | coffee | 5 | 4 |16/11/2015 |
3 | tea | 4 | 10 |20/12/2015 |
4 | juice | 5 | 15 | 1/1/2016 |
5 | coffee | 3 | 5 | 15/3/2016 |
6 | water | 5 | 2 | 16/4/2016 |
and items I sold is in this table
ID |ItemName| PriceOfUnit | NumberOfItems I sold |DateIBought|
1 | coffee | 5 | 6 | 1/1/2016 |
2 | tea | 5 | 9 | 15/3/2016 |
3 | coffee | 4 | 2 | 20/4/2016 |
4 | juice | 5 | 11 | 1/1/2016 |
I need a query , SQL query OR union Query in MS Access to get this result ?
ID |ItemName| NumberOfItems I have |
1 | coffee | 1 |
2 | tea | 7 |
3 | juice | 4 |
4 | water | 2 |
How can I get this result ?
Where NumberOfItems I have = NumberOfItems I bought - NumberOfItems I sold
You didn't provide attribute names or table names and you spec'd two RDBMS products, so here is p-code for your solution:
q1:
SELECT ItemName, SUM(bought) as SumBought from tBought GROUP BY ItemName
q2:
SELECT ItemName, SUM(sold) as SumSold from tSold GROUP BY ItemName
q3:
SELECT q1.ItemName, (SumBought - SumSold) as difference from q1 inner join q2 on q1.ItemName = q2.ItemName

mysql query for conditions on related tables

i have a table 'house' with a house id, a table 'room' with a room id, and a relation table for those two tables
HOUSE
-----
1 | house1
2 | house2
3 | house3
4 | house4
5 | house5
ROOM
------
1 | kitchen
2 | bathroom
3 | garage
HOUSE_ROOMS
------------
id | house_id | room_id | size
=================================
1 | 1 | 1 | 200
2 | 1 | 2 | 300
3 | 2 | 1 | 400
4 | 2 | 2 | 500
5 | 3 | 1 | 500
6 | 4 | 2 | 600
7 | 5 | 1 | 400
8 | 5 | 5 | 300
I'm having trouble writing a query that returns an array of house id's by some combined conditions:
example: get all houses with a kitchen sized between 350 and 450 AND a bathroom sized between 450 and 550
-> result should be an array containing house2
anyone know how i should write this query?
Assuming all your IDs are fixed, the following quick query will work:
SELECT HOUSE_ID
FROM HOUSE_ROOMS
WHERE (ROOM_ID=2 AND SIZE>=450 AND SIZE<=550)
OR (ROOM_ID=1 AND SIZE>=350 AND SIZE<=450)
GROUP BY HOUSE_ID
HAVING COUNT(DISTINCT ROOM_ID)>1