I have a database which has a many to many structure between products and their location in our warehouse (due to the random nature of the products being bought)
I need to match a unique location for the same product to multiple customers who order it.
DB SCHEMA
2 tables
Customer:Cust_ID, Prod_ID
Product:Prod_ID,Locat_ID
There are many locations for the same product and many products in the same location.
Customer:
C1,P1
C2,P1
Product:
P1,L1
P1,L2
P1,L3
SELECT
Customer.Cust_ID,
Customer.Prod_ID,
Product.Locat_ID,
FROM Customer JOIN Product ON Customer.Prod_ID = Location.Prod_ID
Gives
C1,P1,L1
C1,P1,L2
C1,P1,L3
C2,P1,L1
C2,P1,L2
C2,P1,L3
If I group by Cust_ID I get
C1,P1,L1
C2,P1,L1
but I need
C1,P1,L1
C2,P1,L2
Thanks for any insights
Related
I have basically 13 different tables. I have Customer table, a Sale table which is connected to Customer table because a customer can buy a Sell and that same Sale table is also connected to an Employee table because an Employee can Sell a Sale. Then I have an Order table connected to a Vendor table because a single order can be placed to a Vendor just as well as many different orders can be placed to many different vendors. But also the Employee table can be connected to a Shop table because an Employee can work at a single shop just as well as many employees work for different shops. The Employee and Shop table is connected by EmployeeShop which have reference or have foreign keys to both the Employee and Shop table. The rest of the relationships can be shown in the pic below. My problem is that I am not entirely sure that some of my tables that are connected to some of the other tables, for example, Ingredient table that is connected to the OrderLineItem, is entirely right. And also Order table and Vendor which are connected by the OrderVendor. Please any help or advice would be beneficial to actually moving on and forward my model to an physical database.
Here is the EER model
[
Let's try that again:
ingredients(ingredient_id*,ingredient)
order_details (order_id*,ingredient_id*,qty)
orders (order_id*, vendor_id, date)
vendors(vendor_id*,vendor details, etc.)
[ * = (component of) PRIMARY KEY ].
I'm designing a relational database tables for storing data about eCommerce scenario where I need to store
List of Products purchased by a user
List of users who purchased a particular product.
Its a many to many relationship.
So far I could only thinking of doing this.
create a table for storing orders
table recordorders(
userID // foreign key from users table
productID, // foreign key from products table
dateofpurchase,
quantity,
price_per_unit,
total_amount
)
It will act like a junction table.
Is this a good approach and are there any other methods than junction table that are more effective and efficient for querying ?
Your bullets describe two tables, not one. Your junction table is not properly described as two lists. It is a set of order info rows. The junction table you gave holds rows where "user [userID] purchased product [productID] on ...". Ie it records order info. (Combinations of user, product, date, etc of orders.) Given a user or product, you can get the corresponding bullet table by querying the order info table.
However your table probably needs another column that is a unique order id. Otherwise it cannot record that there are two orders that are alike in all those columns. (Eg if the same person buys the same product on the same date in the same quantity, price and total.) Ie its rows probably aren't 1:1 with orders. That's why above I called it an order info table rather than an order table. It records that some order had those properties; but it doesn't record distinct orders if there can be orders with the same info. It's really a many-to-many-to-etc (for every column) association. That is why an order id gets picked as a unique name for an order as further info. This new table would be called an entity table, not a junction or association table. It holds rows where "in order [id] user [user] purchased ...".
PS An order is usually something that can be characterized as an association on/among/between an order id, user, set of order-lines (product, quantity, price & total), and other stuff (date, grand total, etc). The orders are usually relationally characterized by an order entity table on order id with its user, date etc plus an order-line association table on order ids and their order-line info.
PPS It's time for you to read a book about information modeling and database design.
You don't "store" those two things in a table (Junction, or otherwise), you discover them from the raw ("Fact") data:
Using your proposed table:
List of Products purchased by a user:
SELECT productID
FROM recordorders
WHERE userID = 123;
List of users who purchased a particular product:
SELECT userID
FROM recordorders
WHERE productID = 987;
A mysql one to many question: various products and where I sell them. I have a variety of products that I can sell, and several locations to sell them. I have a products table with a Primary Key: product_id. I have a locations table with PK location_id and a field product_id to link to the products table.
So, SELECT * FROM products LEFT JOIN locations on product_id.
I got an ambiguous column error. That doesn't make sense, they should line up.
So: SELECT * FROM products LEFT JOIN locations on products.product_id = locations.product_id.
That worked.
Here's the problem: there are many products I can get but don't currently sell. Some products have no locations. To create a link to add a location, I need the product_id. If I print out $row['product_id'] I am getting the product ids from the locations table, which is blank if they are unsold. I need the product ids from the first table.
This can't be an unusual situation.
Ideas?
You need to specify in your SELECT statement that you want to use the product_id from the products table. Here is a sample of what I am talking about (SQL Fiddle):
SELECT p.product_id, p.description, p.unit_of_measure, l.location
FROM products p
LEFT JOIN locations l on l.product_id = p.product_id
As you can see for product_id 3 there is no location but yet I am still displaying the product_id.
MySQL supports the using clause, so you can write the first version as:
SELECT *
FROM products LEFT JOIN
locations
USING (product_id);
Without the using clause, MySQL needs to know the table where a column comes from. Because there are two tables with the same column name, MySQL is confused.
This also has the advantage that the * only includes the product_id column once, not twice.
In my database I have certain categories of food in a table called foodgroups. Therefore I have fooditems in a different table which relate to these categories. Moreover I created a table with orders called orders, and one "orderfood" table which defines the orders, in terms of how many of what products belong to an order.
My intention is to show the most sold fooditems in each category of food. So I want to get out one column with the categories and next to it a collumn with the fooditem which was most sold of the fooditems in this category.
Select foodgroups.name, SUM(orderfood.fooditem_ID*orderfood.amount)
FROM orders, orderfood, products, productgroups
Where productgroups.productgroup_ID=products.productgroup_ID
AND orders.order_ID=orderfood.order_ID
AND products.product_ID=orderfood.product_ID
Group by productgroups.productgroup_ID`
So far it doesn't show any errors, but I have no clue how to get a column with only the most bought food next to its category where it is the most bought one. Maybe you can help me, thank you :)
BTW, I am using the MySQL workbench.
http://sqlfiddle.com/#!2/4b79c/2
I think you're looking for the MAX function. Add it to your select statement
Select foodgroups.name, SUM(orderfood.fooditem_ID*orderfood.amount), max(orderfood.amount)
FROM orders, orderfood, products, productgroups
Where productgroups.productgroup_ID=products.productgroup_ID
AND orders.order_ID=orderfood.order_ID
AND products.product_ID=orderfood.product_ID
Group by productgroups.productgroup_ID`
That should return you the most ordered food
I have a 'customers' table. Each record contains a customer name, email, address, etc.
I have an 'skus' table. Each record contains the name of an SKU. e.g. "Super Software Bundle".
An SKU represents a purchaseable item at a single price point. The purchased item can include one or more products.
e.g. The SKU record named "Super Software Bundle" may represent two products: product1 and product2.
For usability/ease reasons, other tables need to keep track of not only skus purchased, but the individual products a customer may have access to.
Question: how should I represent the variable sized nature of the SKUs in the database, where an SKU can represent from 1 to n products? Thanks for any help.
You will need a third table, that manages the relation between SKU and products, as a product may be part of more than one SKU (I guess). You have to model a m:n-relation.
product
id
name
price
...
sku
id
name
price
...
sku_product
id
sku_id
product_id
If I'm reading this right, you'll need a SKU table and a products table, and the products table will have a foriegn key to the SKU table.