I am making a new database for an online store and Am trying to figure out how to do the orders table. There are multiple products and each product has many options. I am just wondering the best way to keep track of all this. I keep coming back to a table with all the options for all the items. This seems like a bad thing though. Does anyone have any ideas on how to do this? Any help is much appreciated.
Every product is stored in a row of table product
Every product has zero or N options ( 0 N )
Every order contain one or N products ( 0 N )
You need 4 tables to keep track for all this : options, products , products_orders , orders
I'd start with the table structure below as a base. You will almost definately need more columns than the ones i have included but it seems to me that you need what is below at a minimum.
Order
-----
orderId
someOtherColumn
OrderProducts
-------------
orderId
ProductId
product
--------
productId
productName
productOption
-------------
productId
OptionId
Option
--------
OptionId
OptionName
Related
I'm struggling to articulate what I'm trying to achieve.
I have a product which can be assigned many tags, I then have a subscription table which holds the productID and the tagID.
I'd like to search the subscription table in the most efficient way to see if a product is subscribed to ALL tags (not ANYthat I provide).
I've tried many things including this:
SELECT
productID
FROM productTagSubscription
WHERE tagID IN ( 'id1', 'id2', 'id3', 'id4');
So the product has to be subscribed to all 4 ID's as 4 rows in the subscription table
This is then pulling back any records for ANY id, which isn't what I'm trying to achieve - any advice appreciated!!
Table Structure:
Expected Output
My expected output when passing all 3 or 4 tagID's into the table would give me a list of products that all subscribe to all tags provided.
You can count the diferent tagID for each product.
SELECT productID
FROM productTagSubscription
GROUP BY productID
HAVING COUNT(tagID) = 4; /* total amount of tags */
In many pivot tables, you can find a unique pair key, in this case between (productID, tagID), if that's not the case make sure tagID are DISCTINCT.
HAVING COUNT(DISCTINCT(tagID)) = 4;
as long as I change the COUNT number to match the number of tags I'm looking for
If you want to avoid the number, assuming that all tags are been used in one or another product.
HAVING COUNT(DISCTINCT(tagID)) = (
SELECT MAX(DISCTINCT(tagID))
FROM productTagSubscription
);
HAVING COUNT(DISCTINCT(tagID)) = (
SELECT COUNT(*) FROM tags
);
I'm trying to create a web app based on a few mysql tables and forms.
I have the ADD Shipment page in which the user adds a list of products and a Invoice no., so that in my Shipment_Products table I have
id invoice_no product_code qty
1 34 HP222 4
2 34 HL234 1
I also have a Sold page in which the user adds a list of products sold from his stock, so the table Sold_Products get filled like this
id invoice_no product_code qty
1 1 HP222 2
2 34 HL234 1
I need to have a third table called Stock in which I have to get the total number of items in stock, but I'm stuck on how to auto_generate it based on these two existing tables and then keep it updated.
Any suggestions ?
I don't think what you want is as simple as what you might be trying to short-cut. First, if your company is dealing with FIFO vs LIFO inventory, you take inventory out of counts as they were available for COGS (cost of goods sold) purposes based on the cost from a given vendor at a given purchase time.
You might just have a purchases table showing every item and count received, then, as goods are sold, have another table showing the reduction from each purchase received available quantity out. It is somewhat tricky to deal with, given such a simple scenario as...
Purchase 10 of product "X" from a vendor, then another 4 of product "X" shortly after. Total of 14.
Now, for sale activity, you have one order selling 3, then 2, then 4, then 3. Total of 12 of the 14 sold, but whatever way is computed FIFO vs LIFO, there would be a split of which quantity was accounted for from which originating purchase... Ex: FIFO would be accounted for from the original 10 then 4 puchases.
10 purchase
10 - 3 = 7
7 - 2 = 5
5 - 4 = 1
1 - 3 = -2 -- WRONG... Only 1 of the 3 against the original 10 purchase
1 - (1 of 3) = 0
4 purchase
4 - (remaining 2 of 3) = 2 -- here is the remaining 2 entries from the last sale, leaving 2 left in inventory.
So, no absolute table structure to handle, but I think you should check how the inventory management requirements may need them.
I'm going to try and explain it better.
Sorry for my bad english !
I now have a table received in wich every item in a particular invoice is added when it arrives, so let's say today we receive some items, 3 items and the unique invoice_id or shipment_id (doesn't really matter) is S1. In the table received I have
id shipment_id product_code qty
1 S1 HL223 2
2 S1 XLS21 1
3 S1 BenqWHL 1
I have another similar table called sold wich works the same way, I add a list of items by their product_code and give the sale an ID (for other purposes). So the sold table looks like this:
id sold_id product_code qty
1 B1 HL223 1
2 B1 XLS21 1
Now, I just need to see the items left in stock, either by creating a table in wich I store the items grouped by their unique product_code and just count the entries as qty and then maybe when doing a sale I can substract the qty sold in this table stock ?
I don't care about invoice numbers or IDs, users, etc.
This is the bad way, but at least is the way to begins.
make a table: inventory/Stock or something with the next structure: ID, Item_id, item_quantity.
When purchase/receive, add a row to this table with a positive quantity.
When Sales/output, add a row to this table with a negative quantity.
To calculate the stock perform a query with the sum() of a particular item_id.
I have a table full of products including price, name, date and ID (PK)
I am in need to update the records when there is a price change (Web scraping script), how would i go about this so that a new row is NOT inserted, rather the price and date are updated.....but the previous values need to remain in the DB....so that when i when i go
"SELECT * FROM items WHERE id ='27'";
Output wanted:
$400 12.4.2013
$314 22.4.2013
$250 12.4.2013
The product will be then displayed with all the updated values since the first price was downloaded, effectively it will be a history of the prices for the item...
the actual results i want to achieve would hopefully be
History for "Product 27" is:
To give more context, when i run my script... ./script.php update
all the new data should be inserted or updated....but old values remain
im not too sure how i should approach this....please provide me with some guidance or assistance
The best way to go about this while keeping a tidy database with easily maintainable and readable data would be to take what #KishorSubedi said in the comment and create a log table.
When you update your price in your Products Table, store the old price, along with its date and ID in the Log Table then when you need to look up all the records for that product you can JOIN the Log Table in your query.
Log Table
| ProductID | Price | Date |
| 27 | $300 | 02.1.2013 |
| 27 | $400 | 03.1.2013 |
This way you can have a nice and neat Products table that is not cluttered with multiples of the same product, and an unobtrusive log table that is easily accessible.
Hope this gives you some guidance on building your site and database.
If you want to keep the old values I would suggest saving them in a seperate table and to use the INSERT statement. You are always adding a new row, so you can not bypass an insert or select into or similar statement.
Table structure:
Items
------------------------------
Id primarykey
Name
Price
------------------------------
Id primarykey autoincrement
ItemId index
AddDate index
Price
Now when you scrape the web you will just insert the new price in the Price table. If you want to select all the prices for an item you can use
SELECT Items.Id
,Items.Name
,Price.Price
,Price.AddDate
FROM Items
LEFT JOIN Price
ON Items.Id=Price.ItemId
WHERE Items.Id='27'
ORDER BY Items.Name ASC, Price.AddDate ASC
And if you just want the latest or current price, you can use
SELECT Items.Id
,Items.Name
,P1.Price
,P1.AddDate
FROM Items
LEFT JOIN Price P1
ON Items.Id=Price.ItemId
LEFT JOIN Price P2
ON P1.ItemId=P2.ItemId
AND P1.Id<P2.Id
WHERE Items.Id='27'
AND P2.Id IS NULL
Add new column for history in 'item' table and each price change append the changed value to this column with a specific format as like date1:price1,date2:price2,....
What is the best practice to do a searching form with filters, where the filters are depending on the category?
e.g.:If you go to visit the ebay and select a category then the filters are different on the left hand side in the cell phones (filters: brand, operating system...) or the fashion category (filters: size, color...)...
In my mind I would do more table in DB. Each table for one category (cat_cellphone, cat_fashion...). Then put a product one of these tables depending on the category (not one products table where one column contains the category ID).
These tables are different where the columns names are characterized by the category.
Next, should do more searching forms and call a form where the filter belongs to the category.
Is it a good concept or there is an other accepted practice in big projects?
No, having multiple tables is a bad idea.
In general. use a table with a primary key over two columns instead.
(The primary key may span more than one column.)
The columns would be categoryname / filtername.
If you don't like such primary keys, and always use an autoincrement column, you can still create an index over the two columns.
The columns would be: id / categoryname / filtername / filtertext
Using multiple tables to store your products isn't a good idea. Because there will be products that overlap in categories which would result storing the same product in more than one table.
Just use a product table with the ID, product_number, description, etc. and a category table to store the different categories. Then you could link them directly like:
Product table:
ID product_number description category
1 00001 Screwdriver 1
Category table:
ID description
1 Tools
And you could even expand the category table with an extra column to use subcategories by addressing the parent of the subcategory:
Category table with subcategories:
ID description parent
1 Tools NULL
2 Automatic tools 1
And if you don't like directly linking to the category table from the product table you could use a link table:
Product_category:
Product_ID Category_ID
1 1
Hope this answers your question.
Edit, added filter table:
To add filtering for a product you can use a table for a the filters and a link table, like so:
Filter:
Filter_ID description value
1 brand Bosch
2 brand Bahco
3 type Phillips
Product_Filter:
Product_ID Filter_ID
1 1
1 3
That way you can link more than one filter to each product and use the same filter more than once.
You could even expand this further by using another table for the filter values, but that could make things a bit to complicated:
Filter:
Filter_ID description value
1 brand 1
Filter_value:
Filter_ID value
1 Bosch
2 Bahco
I am trying to fetch data from multiple tables depending on what is selected in a dropdown menu. My dropdown menu consists of a list of ID's (001, 002, etc).
Once a user selects one of them, I am using AJAX to dynamically fetch data depending on what was selected. I was able to fetch a single value depending on what was selected but having problems when multiple tables are involved.
My tables are set up like this:
Inventory table:
inven_ID (primary)
cost
description
Order table:
order_ID(primary)
orderdesc
Sale table:
inven_ID
order_ID
quantity
primary(inven_ID,order_ID)
My query is as follows:
$QRY = "SELECT
inven_ID,
order_ID,
cost,
description
FROM
Inventory,
Order,
Sale
WHERE Inventory.inven_ID = Sale.inven_id
AND Sale.order_ID = Order.order_ID
AND Order.order_ID ='".$q."'";
The $q represents the value from the dropdown menu (which I checked is valid). I am getting the error Column 'inven_ID' in field list is ambiguous. Basically, when they select some order id from the drop down (say 001), it looks for order_ID in my Order table, and fetches the inven_ID/cost/description of that particular order ID.
Eg. if someone ordered parts xy, yz, xyz for cost 10,20,30.
Selecting 001 would bring up:
001 xy 10
001 yz 20
001 xyz 30
I think I am not joining tables properly since the error says its ambiguous.
Any help on this?
edit: yes that fixed the problem, quite obvious that I did not catch it.
In the column list of your select, you just need to specify which inven_ID you want to retrieve. For instance:
SELECT Inventory.inven_ID, ...
The error is pretty obvious. inven_ID is ambiguous because you have it in Sale and in Inventory. Use a specifier like Sale.inven_ID or Inventory.inven_ID.