How to JOIN four tables - mysql

I have four tables orders, items, projects, and stores. I would like to join them together.
This is my orders table:
My expected result will look like this:
Also this is my naked query without join.
SELECT orders.ID,orders.Quantity, items.Desc, stores.store_name, projects.Project_no
FROM orders, items, stores, projects

You need to use a join on the primary keys and foreign keys of other table.
for eg.
SELECT orders.ID,orders.Quantity, items.Desc, stores.store_name, projects.Project_no
FROM orders
join items ON (orders.orderId = items.orderid)
join stores ON (items.storeID = store.storeID)
join projects ON (projects.projectId = store.projectId)
Note : this is assume some primary keys.

Assuming the ID columns are named the same in the other tables,
SELECT orders.ID, orders.Quantity, items.Desc, stores.store_name, projects.Project_no
FROM orders
join items on items.item_id=orders.item_id
join stores on stores.store_id=orders.store_id
join projects on projects.project_id=orders.project_id

SELECT orders.ID,orders.Quantity, items.Desc, stores.store_name, projects.Project_no FROM orders o
inner join items on items.item_id - o.item_id
inner join stores on stores.store_id = o.store_id
inner join Projects on projects.project_id = o.project_id
This should work, however i have made assumptions about your id columns in your additional tables.

Related

Join on multiple tables using mysql and many foreign keys

Using MySQL, I have 3 tables as follows:
locations (id, name)
products(id, description)
inventory_records(id, product_id, move_from, move_to)
product_id is a FK to products; move_from and move_to are FK to locations.
The following query lists all products names and their origins.
select
products.description,
locations.name
from
inventory_records
inner join products on
products.id = inventory_records.product_id
inner join locations on
locations.id = inventory_records.move_from
limit 10;
But I wanna list both origin and destinations, which I couldn't compose the query. Any helps?
You will need to Join the locations table twice. First Join will be on move_from; and second table join would be on move_to.
Also, note that it is a good practice to use Aliasing in case of multi-table queries, for code clarity, readability and unambiguous behavior.
SELECT
products.description,
lfrom.name AS move_from_location,
lto.name AS move_to_location
FROM
inventory_records
INNER JOIN products ON
products.id = inventory_records.product_id
INNER JOIN locations AS lfrom ON
lfrom.id = inventory_records.move_from
INNER JOIN locations AS lto ON
lto.id = inventory_records.move_to
LIMIT 10;

Inner Join for list of records NOT in a second table (NOT Inner Join)

I have a table let's call it products with a list of Manufacturers and Products.
I have a second table let's call it Customer, Orders.
I can do a join to make a list of all the items from each manufacturer the customer ordered doing an Inner Join. Yet trying to do an Inner Join for the items they did not fails.
I tried an Inner Join with 'Orders.Product != Products.Product' but that only works where the Customer has one order. Once there is more than one order I get the same list I would have doing an Inner Join. Any thoughts? I'll try to make a SqlFiddle tonight but was hoping a quick description might help a MySql / Join expert who has done 'NOT Inner Join'before...
It is called an anti join, you can use left join with is null check:
select p.*
from products p
left join orders o on p.Product = o.Product
where o.product is null

mysql joins with many to many relationship

I am trying to learn joins with many to many relationships in mysql,
I have four tables:
customers, orders, products, payments
I am trying to get records as:
customer_name, order_status, pay_method, pro_name
the query I use:
SELECT cust_name,order_status,pay_method,pro_name FROM customer
INNER JOIN orders ON customer.cust_id = orders.cust_id
INNER JOIN payments ON payments.order_id = orders.order_id
INNER JOIN products ON products.pro_id = orders.pro_id
I am receiving results as I want with no issue. But this query shows only one product against one order, then I realize I should have another separate table which will hold many products against one order. In this issue I am not able to get desired result
It's not entirely clear what you're asking, but I'm guessing what you're trying to do is create a many to many table that links orders and products? In which case, you can just create a table called "productorders" which will contain an order_id and a pro_id. Then you would modify your query like this:
SELECT cust_name,order_status,pay_method,pro_name FROM customer
INNER JOIN orders ON customer.cust_id = orders.cust_id
INNER JOIN payments ON payments.order_id = orders.order_id
INNER JOIN productorders ON productorders.order_id = orders.order_id
INNER JOIN products ON products.pro_id = productorders.pro_id;
Joining productorders will get all products associated with an order, and then joining products will get the information associated with each product.

Need to join MySql query to 3 tables

SELECT products.acctnum,products.subacctnum,NOW(),
items.amount,items.id,items.invoice_id,items.product_id,
items.po_id, invoices.customer_id, purchaseorders.vendor_id FROM items
INNER JOIN (products, invoices, purchaseorders)
ON (items.product_id=products.product_id AND items.invoice_id=invoices.id
AND items.po_id=purchaseorders.id)
This returns nothing... however..
SELECT products.acctnum,products.subacctnum,NOW(),
items.amount,items.id,items.invoice_id,items.product_id,
items.po_id, purchaseorders.vendor_id FROM items
INNER JOIN (products, purchaseorders)
ON (products.product_id=items.product_id AND purchaseorders.id=items.po_id)
Works...
SELECT products.acctnum,products.subacctnum,NOW(),
items.amount,items.id,items.invoice_id,items.product_id,
items.po_id, invoices.customer_id FROM items
INNER JOIN (products, invoices)
ON (products.product_id=items.product_id AND invoices.id=items.invoice_id)
Works...
Works for the rows I need in the result but when I join the 3rd table it doesn't work. LEFT JOIN displayed all the columns I needed but some rows were NULL.
I imagine the join clause you want looks more like this:
FROM items LEFT JOIN
invoices
ON invoices.id = items.invoice_id LEFT JOIN
purchaseorders
ON purchaseorders.id = items.po_id LEFT JOIN
products
ON products.product_id = items.product_id
I'm not sure which fields are not valid when you select them, but you can probably fix such issues by using coalesce() with appropriate fields from invoices and purchaseorders.

Query for multiple tables

I'm trying understand how I can pull information from multiple tables at once in one query if that is possible.
I have 3 tables and I'm wondering if there is a way I can query all the product names for customers that live in california?
Table:
products
Fields:
productOid
productName
companyOid
Table:
customerData
Fields:
customerOid
firstName
lastName
state
Table:
orders
Fields:
orderNumber
customerOid
productOid
Would this fall under something like an INNER JOIN?
Also, I'm learning mySQL.
You will need to use inner joins for this.
SELECT DISTINCT p.productName
FROM orders o
INNER JOIN customerData c ON o.customerOid = c.customerOid
INNER JOIN products p ON o.productOid = p.productOid
WHERE c.state = 'CA';
I am using DISTINCT here because it's possible a customer would order the same product more than once (or multiple customers would order the same products) and I'm assuming you don't want duplicates.
I'm also making the assumption that your state is represented as a two character column.
Read more about joins
You could use one more join, but I would write it this way:
SELECT DISTINCT p.productName
FROM
orders o INNER JOIN products p
ON o.productOid = p.productOid
WHERE
o.customerOid IN (SELECT customerOid
FROM customerData
WHERE state = 'California')
It might be a little slover than a join, but it's more readable.
This shows products that CA customers have ordered:
SELECT p.productName
FROM orders o
INNER JOIN products p ON o.productOid = p.productOid
INNER JOIN customerData c ON o.customerOid = c.customerOid
WHERE c.state = 'CA'