Join 4 Tables in SQL with left join and inner join - mysql

Hey I am trying to join 4 tables in sql, with an left join and an inner join.
Hospital table
HospitalID| Name| Province| Email|
Order table
OrderID| HospitalID| StaffID| DeliverID| Date| Time
Item table
ItemID| Type| Name| Quantity| Expiry_Date
OrderItem table
OrderItemID| OrderID| ItemID| Quantity
I attempted executing the following SQL query but I am getting error message and I don't know what I'm doing wrong.
SELECT Hospital.Name, Item.Type, OrderItem.Quantity
FROM Hospital
LEFT JOIN [Order]
ON Hospital.HospitalID=[Order].HospitalID
INNER JOIN (SELECT Item.Type
FROM Item
GROUP BY Item.Type)
OrderItem ON Item.ItemID = OrderItem.ItemID
;

There are few mistakes in the query, the syntax using [Order] is invalid in mysql, and then you need an alias for the inner query. Also OrderItem needs to be joined first with Order. Further more no need to use use subquery for join since you are not doing any aggregate part to get the data from Item. In mysql the query will look like below.
SELECT
h.Name,
i.Type,
oi.Quantity
FROM Hospital h
LEFT JOIN `Order` o ON o.HospitalID = h.HospitalID
INNER JOIN OrderItem oi on oi.OrderID = o.OrderID
INNER JOIN Item i
on i.ItemID = oi.ItemID ;
Note that Order is a reserved word so you need to backtick it as done in the query above

you can use below simple query-
SELECT
hosp.Name,
itm.Type,
oi.Quantity
FROM Hospital hosp
JOIN `Order` ord ON ord.HospitalID = hosp.HospitalID
JOIN OrderItem oi on oi.OrderID = ord.OrderID
JOIN Item itm on itm.ItemID = oi.ItemID;
If there is chance that you want to show all hospital name even without its details in other tables like type, quantity etc. then you can use of left join-
SELECT
hosp.Name,
itm.Type,
oi.Quantity
FROM Hospital hosp
LEFT JOIN `Order` ord ON ord.HospitalID = hosp.HospitalID
LEFT JOIN OrderItem oi on oi.OrderID = ord.OrderID
LEFT JOIN Item itm on itm.ItemID = oi.ItemID;

Related

Using cases to determine which table should join

I have four tables products, product_histories, vendor_invoices and invoices
This is the query I have developed
SELECT p.product_id, product_name, vendor_name FROM products AS p
INNER JOIN product_histories AS ph ON p.product_id = ph.product_id
CASE
WHEN ph.history_type = "P" THEN
LEFT JOIN vendor_invoices AS vi ON link_id = vi.vi_id
WHEN ph.history_type = "S" THEN
LEFT JOIN invoices AS i ON i.invoice_id = link_id
END
ORDER BY ph_id ASC
What I want that if ph.history_type is P then is should join vendor_invoices and if it is S then it should join invoices. But it says there is a syntax error.
Can anyone help me out with it? Or could show a better way to achieve this problem.

How to join multiple table where two tables are based on third table

I am new in MySql and am trying to join multiple column from 4 tables where two tables (order and product) linked by third table i.e orderline.
Here is the query which I think is not correct.
Select orders.OrderDate, orders.OrderID, orders.OrderStatus, customer.FirstName, customer.LastName, product.ProductName , product.Qunatity, branch.BranchName
from orders
inner join customer ON orders.CustomerId = customer.CustomerId
inner join orderline ON orderline.OrderId = orders.OrderID
inner join branch on orders.BranchID = branch.BranchID
inner join orderline.ProductId = product.ProductId;
But I am getting error which of 1054, unkwon column 'product.ProductName' in 'fieldlist'
Can anyone show me the right way to do this.Thanks
Looks like a typo in the last JOIN. Is this what you meant?
SELECT orders.OrderDate, orders.OrderID, orders.OrderStatus, customer.FirstName, customer.LastName, product.ProductName , product.Quantity, branch.BranchName
FROM orders
INNER JOIN customer ON orders.CustomerId = customer.CustomerId
INNER JOIN orderline ON orderline.OrderId = orders.OrderID
INNER JOIN branch ON orders.BranchID = branch.BranchID
INNER JOIN product ON orderline.ProductId = product.ProductId;

MySql crazy join thorugh a grouping table

I have a database structure with the following setup:
po: id, stockNumber, factoryId, other columns
order: id, stockNumber, factoryId, other columns
stock_number: id, stockNumber, groupId
factory: id, name, groupId
The important part here is the stock_number/factory tables. The groupId column is just an integer and if two or more rows in the table have the same value then their stock numbers/factory are considered the same. Typically this is used for different sizes of the same product.
What I'd like to do is write a query that will join "order" to "po" through the group of stock_number and factory so I can find orders with no matching po. Also the factory has to match the same way.
I have this query if I have a specific stock number/factory in mind but I'd like to update it to query the whole orders table for me:
SELECT id
FROM order
WHERE
styleNumber IN (SELECT a.stockNumber FROM stock_number a INNER JOIN stock_number b ON a.groupId = b.groupId or a.id = b.id WHERE b.stockNumber = '123')
AND factoryId IN (SELECT a.submitter_id FROM submitter a INNER JOIN submitter b ON a.groupId = b.groupId OR a.submitter_id = b.submitter_id WHERE b.SUBMITTER_ID = 'alpha');
EDIT: I came up with this query which I think might be on the right track. It only joins in the stock number so it doesn't do factory yet. Can anyone confirm if I'm going in the correct direction:
SELECT *
FROM order o
LEFT JOIN stock_number s_o ON o.stockNumber = s_o.stockNumber
LEFT JOIN stock_number s_p ON s_o.groupId = s_p.groupId
LEFT JOIN po p ON s_p.stockNumber = p.stockNumber
WHERE p.id IS NULL;
Just join all the tables.
select o.id
FROM order AS o
JOIN stock_number AS sn ON sn.stockNumber = o.stockNumber
JOIN submitter AS su ON ON o.factoryId = su.submitter_id
You could use an anti-join pattern. In this example, it looks complicated because of the two relationship tables. But a query something like this:
SELECT o.id
, o.stockNumber
, o.factoryId
FROM `order` o
LEFT
JOIN `stock_number` s
ON s.stockNumber = o.stockNumber
LEFT
JOIN `factory` f
ON f.id = o.factoryId
AND f.groupId = s.groupId
LEFT
JOIN `po` p
ON p.stockNumber = s.stockNumber
AND p.factoryId = f.id
WHERE p.id IS NULL
The anti-join pattern is easier to visualize with a simpler example. Say you had the order table (as in your example), and an order_line table, with rows related to the order table by the order_id column.
order_line: id, order_id, othercolumns
To get order along with matching order_line rows:
SELECT o.id AS order_id
, l.id AS line_id
FROM `order` o
JOIN `order_line` l
ON l.order_id = o.id
To include rows from order that don't have any matching rows in order_line, we can use an outer join. We add the LEFT keyword:
SELECT o.id AS order_id
, l.id AS line_id
FROM `order` o
LEFT
JOIN `order_line` l
ON l.order_id = o.id
That gets all rows from order, including rows that don't have a matching row in order_line. The trick now is to exclude all the rows that have a matching row. For any rows that didn't have a match, the columns from order_line will be NULL. So we can add a test in the WHERE clause, to exclude rows that had a match.
SELECT o.id AS order_id
, l.id AS line_id
FROM `order` o
LEFT
JOIN `order_line` l
ON l.order_id = o.id
WHERE l.order_id IS NULL
That gets us rows from order that don't have a matching row in order_line.
We can use this same pattern in a more complicated query. We use outer join operations, and rows from order that don't have a matching row in po will have NULL values for the columns from po.

SQL Querying multiple tables using JOIN - Northwind

//Personal understanding, not a hw assignment
So in the sample db northwind from MS there are the tables: orders o, [order details] od, customers c
o has orderID, customerID (inc. duplicates)
od has orderID (inc. duplicates), unitprice, quantity, discount
c has customerID, companyName
roughly speaking,
I want to join on
o.customerID = c.customerID; selecting companyName ///
join on o.orderID = od.orderID; selecting unitprice, quantity, discount
my end goal is to
sum(q (up - d)) AS 'Order Total' group by od.orderID then
sum(Order Total) group by companyName(?)
My main issue is not know how/what to join properly though.
Thanks in advance
Check out your scenario on SQL Fiddle
SELECT comp.`company_name` AS 'company',COUNT(DISTINCT o.id_sales_order) AS 'total_orders',SUM(`unit_price`) AS 'grand_total'
FROM sales_order AS o
LEFT JOIN sales_order_item AS od ON od.fk_sales_order = o.id_sales_order
LEFT JOIN customer AS c ON c.id_customer = o.fk_customer
LEFT JOIN company AS comp ON comp.id_company = c.fk_company_id
GROUP BY comp.`company_name`
hope this what you are looking for
Assuming that you create the correct Select statement as you required, the join part should be something like:
From Orders o join Order_Detail od on o.orderID = od.orderID
join Customer c on o.customerID = c.customerID
Types of join can be: join, inner join, right/left join. It depends on you what do you want to achive i.e if you want to exclude/include null references. But the structure is the same.

SQL query for 3 tables

I have 3 tables as follows
item{id, name, price}
customer{id, name, tel_no}
order{id, time, customer_id}
order_item{id, item_id, price, order_id}
process{id, order_item_id, status}
I need to get order_items which are not processed for a particular customer. I tried with the following query. But it doesn't help. Please correct me some one.
SELECT *
FROM order_item`
INNER JOIN `order` ON `order`.id = order_item.order_id
WHERE `order`.customer_id=1 AND NOT EXISTS (
SELECT *
FROM process
WHERE process.order_item_id=order_item.id
)
I'm using mysql as my server
Select *
from order_item OI
INNER JOIN ORDER O on O.ID=OI.Order_Id
LEFT JOIN Process P ON P.Order_item_ID = OI.Item_ID
where O.Customer_ID = 1 and P.ID is null
LEFT JOin gives you all the ORDER_Items and only those records with a matching process record so P.ID will be null thus the item has not been processed