I don't know if this question should be here or not but i don't see any other place fit to ask my problem.
See, i am developing an online food portal which obviously sells food , snacks ,cakes and desserts.The real deal comes up when i have to sell a cake which can exist in various weights such as 0.1|0.5|0.9|...4.5 and every one of them costs different.Furthermore, every one of them are present in different quantities in warehouse.
My solution to achieve this was to provide a different row for every variant
ID | product_code | product name | Quantity | price | weight | company_id
1 | 12345 | beer cake | 34 |345 |0.5 |343434defee
2 | 12345 | beer cake | 343 |600 |1.0 |343434defee
3 | 12345 | beer cake | 4 |845 |1.5 |343434defee
4 | 12346 | vodka cake | 341 |345 |0.5 |343434deereee
Here , i looked every product to be a different product,I provided the relation using product code while to differentitate every row , i choose id,product_code as the primary key.
But, using this method involves redudancy as well, every row is looked as a different product.So if i were to display them using php it would show them as different product.
Something like this,
How do i change my structure of my database in order to achieve a single product but with different variants option on the same item?
I could use a php solution , if it's there.
Regards,
BOTjr.
Split the table into two with product_code as foreign key.
Table One
ID | product_code | product name | company_id
1 | 12345 | beer cake |343434defee
2 | 12346 | vodka cake |343434deereee
Table Two
Product_code |Quantity | price | weight
12345 | 34 |345 |0.5
12345 | 343 |600 |1.0
12345 | 4 |845 |1.5
12346 | 341 |345 |0.5
Use Table One to display items, Use Table Two is product description page to select the right quantity
Related
Ok, I hope I can make this question as clear as possible.
I have the following table:
table: phones
phone_id | name | ... ... ... | manufacturer_code
-----------------------------------------------------
1 | samsung | | 001
2 | apple | | 002
3 | htc | | 003
and so on...
I have A LOT of columns (... ... ... means at least 40 different columns like width, height, depth, color, has_bluetooth, ... stuff like that).
Now I want to do this differently, but I have NO IDEA how to start.
How I THINK to do it? (correct me if i'm wrong!)
1) Store the field names in a different table
table: phone_fields
field_id | name
--------------------------------
1 | name
2 | width
3 | weight
4 | depth
... | ...
41 | manufacturer_code
2) Connect the fields with the data for each phone in another table
table phones
row_id | phone_id | field_id | value
------------------------------------------------------
1 | 1 | 1 | samsung
2 | 1 | 2 | 10,50 cm
3 | 1 | 3 | 1 kg
... (and so on).
I want this to be searchable/filterable, I have a filter now which filters on brand, color, pricerange, ... and this works fine.
How should you guys do it? Or does anyone have a useful link/tutorial about this? English is not my native language so I don't know exactly how to search for it.
Thanks in advance!
Edit: why am I doing this? I want to be able to add extra fields through my admin-panel if necessary (and not via phpmyadmin or something like that).
If your request always returns the entire set of fields, then leave it in one table
If not, then you can leave only the most important fields in one table, and in the other table place the remaining attributes and link it via foreign key
I've been thinking about this for a while now, but I am still not sure as the best way to go about it.
So basically, in my online shop I have an order table:
+------+-------+---------+-------------+----------------+--------------+--------+
| id | name | email | grand-total | shipping-costs | address | status |
+------+-------+---------+-------------+----------------+--------------+--------+
| 4312 | Roger | r#g.com | 5.99 | 1 | 22 Road Lane | 1 |
+------+-------+---------+-------------+----------------+--------------+--------+
And linked to that table is the cart table:
+-----+---------+-----------+---------------+-------+----------+--------+
| id | orderID | productID | name | price | quantity | status |
+-----+---------+-----------+---------------+-------+----------+--------+
| 234 | 4312 | G55Sd | Super Product | 5.99 | 1 | 1 |
+-----+---------+-----------+---------------+-------+----------+--------+
This all works correctly. However, the problem comes when I want to start adding discounts. I want to be able to look at a past order, and know exactly what discount was applied, how much etc. But I don't know where to save it. And what if the customer has multiple discounts? I can't just add another column in the orders table for discount, or discount amount, as that wouldn't help future me look over it at all.
I was thinking maybe I could add a line in the cart table for that order, and apply it like a product, just with a minus amount. I could do with some advice please.
Be careful you don't over-normalise. Disk is cheap - I would consider holding discount details in the order table and the cart table. The business logic could be every product might accrue a discount and the order itself might have a discount on top of the product discount(s).
I'm bending my mind for some time now over this problem. Could someone please help me?
I have two tables: products and product_attributes. The product has all basic product information and product_attributes has all specific information for products on different categories. It's much like the magenta attribute system. this table has 4 columns: id, product_id, attribute_name, attribute_value.
Now let's say a product has 2 attributes:
------------------------------------------------------
| id | product_id | attribute_name | attribute_value |
------------------------------------------------------
| 1 | 123 | length | 123cm |
------------------------------------------------------
| 2 | 123 | material | Denim |
------------------------------------------------------
| 3 | 123 | season | Summer |
------------------------------------------------------
Now if I set up the eloquent relationships and query a product, I get a product object with all three attributes. So far this is what I wanted. But now in a blade template I would like to be able to do something like this:
$product->attribute->length
Is this even possible or do I need to achieve these kind of things with a total different approach (like creating different tables for different product types/categories)?
Thanks in advance!
length is a tuple value not an attribute you need
$product->attribute->where('attribute_name', 'length')
or
$product->attribute->whereAttributeName('length')
I have differents address book in excel format, i have imported all (around 20.000) into a mysql table (address_tmp) now i have many duplicated records and need to remove that and to move the clean records into the mysql table (address_book). The duplicated items its the minus problem the big problem is to choose wich records to delete and to merge some data into ad unique and most complete record.
es.
1 | Tom Cruise | tom#cruise.com | +17777777 | category 1
2 | Tom Cruise | empty | +177777777 | category 2
3 | Cruise | tom#cruise.com | empty | category 3
i would like to keep record 1 that is the most complete and to merge the categories with this result:
1 | Tom Cruise | tom#cruise.com | +17777777 | category 1, category 2, category 3
Is it possible by some mysql+php code or it is a work to do by hand.
Thanks
I am trying to find a good solution to accomplish the following:
I have a table which includes the name of various products, such as:
"Tide - Original Scent".
In addition, I also have the amount in there for e.g. 50 fl oz.
The problem I have right now is, that the product not only comes in containers of 50 fl oz but also in different sizes such as 75 fl oz and 100 fl oz. For each of these I have new rows in the product table:
id| productName | amount | unit
1 |"Tide - Original Scent" | 50 | "fl oz"
2 |"Tide - Original Scent" | 75 | "fl oz"
3 |"Tide - Original Scent" | 100 | "fl oz"
Now I have a web interface to perform a search on this table and whenever I search for "tide" I get all three rows - which is supposed to be like that of course. However I would like a different behavior and this is where I need your help:
Instead of returning all three rows I would like one row only. I would then need to be able to process it in php so that if the user clicks on "Tide - Original Scent" that the user is then prompted to select the size.
To add even more complexity to the task:
I also have products in the same table named:
"Tide - Mountain Spring".
In this case, it would be great to have some relations set up so I know that "Tide - Original Scent" is linked with "Tide - Mountain Spring". Within php I would then like to not only give the user the choice of selecting the size but also the (in this case) scent.
What would your recommendation be on how I can accomplish this (not the php part)?
How would your database look like?
Do I need to create a table where I map these products? How would this look like if you would create this :)
2 possibilities:
Don't store the sizes in that table - along with the other specific information. Move that to another table. Denormalize your structure.
or
Query but group by the name. For the size column, do a count(amount). If it's more than one, you can then populate a drop down with choices. This is good temporary fix.
SELECT productName, count(amount) AS numOfChoices FROM YOUR_TABLE
WHERE LOWER(productName) LIKE 'tide%'
GROUP BY productName
then after the choice is made
SELECT id, amount FROM YOUR_TABLE
WHERE id = "$selectedId"
to present a choice of sizes that will pin point which one.
I would personally setup my tables like this..
Products Table:
ID| Product ID | Product Name | Description
1 | 0404888282 | Tide - Original Scent | Smells Good
Quantity Table:
ID| Product ID | Size| Price | Quantity
1 | 0404888282 | 50 | 4.99 | 23
2 | 0404888282 | 75 | 5.99 | 120
3 | 0404888282 | 100 | 7.99 | 10
This structure you have a table for each unique item, another for the sizes and quantity of each size. Keeps the structure clean and easy to understand.
"Instead of returning all three rows I would like one row only."
SELECT DISTINCT productName FROM YOUR_TABLE WHERE LOWER(productName) LIKE 'tide%'
And you'll need a functional index on LOWER(productName) for good performance. Alternatively a case-insensitive collation sequence could be used on DBMSes that support that (e.g. MS SQL Server).
"I would then need to be able to process it in php so that if the user clicks on "Tide - Original Scent" that the user is then prompted to select the size."
SELECT amount FROM YOUR_TABLE WHERE productName = whatever_user_selected
"To add even more complexity to the task: I also have products in the same table named:
"Tide - Mountain Spring"."
The query above will also return that.
What you can do is :
$Query = 'SELECT productName, amount, unit FROM products';
$Data = array();
while($Assoc = mysql_fetch_assoc($Query)){
if(!isset($Data[$Assoc['productName']])){
$Data[$Assoc['productName']] = array();
}
$Size = $Assoc['amount'].' '$Assoc['unit'];
$Data[$Assoc['productName']][] = $Size;
}
// Now what you can do is :
foreach($Data as $ProductName => $Amount){
echo $ProductName.' has :<br />';
if(count($Amount) > 0){
foreach($Amount as $Key => $Value){
echo $Value.'<br />';
}
} else {
echo 'Nothing<br />';
}
}
This however doesn't solve the problem on MySQL's side. IT will work in PHP wihtout problem. It's not beautiful but it's working.
For the first problem, you could create two tables:
products - this is where you store all the information about a product except for the specifics such as different sizes, colors, etc.
attributes - you would link to the product and for each attribute you specify a value
products
id | description
---+------------
1 | crazy shirt
2 | clown shoe
attributes
product | name | value
--------+-------+-------
1 | color | green
1 | color | blue
1 | size | medium
2 | size | large
You can optimize the attributes table further by creating a attribute_names table (and even an attribute_values table).
For your second problem, you could either:
create a related product id column inside the products table; this would limit you to only one related product per product.
create a related product table in which you store combinations between two products.
related_products
product_id | related_product_id
-----------+-------------------
1 | 2
1 | 3
That would create a relationship between product 1 and products 2 and 3.
Hope this helps.
Create a foreign key to sublabels, and order them with a counter. This will look like.
Product Table:
Product ID (key)
Brand ID
Price
Brand Table:
Brand ID (key)
Brand
Sublable Table:
ID
Product ID
Order Index
Value
Size Table:
Size ID
Value
Unit
ProductSize Table:
Size ID
Product ID
Then, you'll divide into subcategories using subsequent sublabels.
Products
10 | 6 | 1.99
11 | 6 | 2.99
12 | 6 | 3.99
13 | 6 | 1.99
14 | 6 | 2.99
15 | 6 | 3.99
Brand
6 | Tide
Sublabel
30 | 10 | 1 | Original Scent
30 | 11 | 1 | Original Scent
30 | 12 | 1 | Original Scent
30 | 13 | 1 | Mountain Spring
30 | 14 | 1 | Mountain Spring
30 | 15 | 1 | Mountain Spring
Size Table
1 | 50 | fl.oz.
2 | 75 | fl.oz.
3 | 100 | fl.oz.
Product Size Table
1 | 10
1 | 13
2 | 11
2 | 14
3 | 12
3 | 15