How to use select statement within select statements if condition - mysql

What I am trying to do:
I want to get the name of the product with it's id from orders table , but in my case I have stored products in 3 different tables.
i.e dish,drinks,others.
Problem:
So the problem is in orders table i have put a type(what kind of product it is) like "dirnks,dish,others".So how can i get the name of the product when product can be in any tables(dish,drinks,others)
What i want:
I don't whether it's possible or not but if anyone can tell what below query is there,how can i use select statement in IF statment
SELECT product_id,type,quantity ,
( IF(type='Drinks)
THEN
(SELECT drink_name FROM drinks WHERE drink_id=product_id)
ELSE IF (type='Dish)
THEN
(SELECT dish_name FROM dish WHERE dish_id=product_id)
)
as drink_name
FROM order_product
WHERE shop_id='JSMMSK730'
Is this thing possible or not?

Use a UNION, and add a new column to the subquery that indicates which table the rows came from. Then you can use this in the JOIN condition.
SELECT product_id, o.type, quantity, name FROM
order_product o JOIN
(
SELECT "dish" type, dish_id id, drink_name name from dish
UNION
SELECT "drinks" type, drink_id id, drink_name name from drinks
UNION
SELECT "others" type, other_id id, other_name name from others
) p
ON p.id = o.product_id and p.type = o.type
WHERE shop_id = 'JSMMSK730'

Try below Syntax:
SELECT * FROM
(
SELECT product_id, product_name from dish
UNION
SELECT product_id, product_name from drinks
UNION
SELECT product_id, product_name from others) as product, order
)
WHERE product.product_id = order.order_id

Related

How to match up dates on 2 different tables and join to an ID table?

I have a table full of product ids and their attributes. I want to join sales data and receipt data from 2 different tables and a different row for each id and date combo. So I want the output to look like this:
I tried joining the product id table and sales table to the receipt table but I'm not sure how to get the dates from the sales and receipts table to match up. Not sure of how to approach this. Thanks!
Calculate the counts for each table and combine them usung UNION ALL
select
product_id
,sales_date
-- combine the counts from both tables
,sum(sales_count)
,sum(receipt_count)
from
(
-- get the counts for the sales table
select
product_id
,sales_date
,count(*) as sales_count
-- UNION needs the same number of columns in both Select -> adding a dummy column
-- 1st Select determines the datatype
,cast(0 as int) as receipt_count
from sales
group by product_id, sales_date
UNION ALL
-- get the counts for the receipts table
select
product_id
,receipt_date
,0
,count(*)
from receipts
group by product_id, receipt_date
) as dt
group by product_id, receipt_date
select p.product_id, s.sales_date, s.sales_count, r.receipt_count
from
products p,
(select count(*) sales_count, sales_date, product_id from sales group by 2,3) s
(select count(*) receipt_count, receipt_date, product_id from receipts group by 2,3) r
where
p.product_id = s.product_id
and p.product_id = r.product_id
and s.sales_date=r.receipt_date
;

Retrieve the rows which has matches for all the rows of another resultset in mysql

The following are the tables of my application
Product(product_id(pk),name,price)
Customer(customer_id(pk),customer_name,address)
Purchases(customer_id(fk),product_id(fk),quantity);
select count(product_id),customer_id from purchases
where product_id in (
select product_id from product where price >200)
group by customer_id
i'm able to do it up to here where the above query retrieves count of the products for each customer. i even tried the below one
select sub.customer_id from (select count(product_id) as prod_count,
customer_id from purchases where product_id in
( select product_id from product where price >200)
group by customer_id) as sub
having sub.prod_count=(select count(product_id) from product where price>200);
Now how can i find the customers who purchased all the products with price above 200.
SELECT DISTINCT customer_id
FROM (
SELECT PUR.customer_id, PUR.product_id
FROM (SELECT product_id FROM Product WHERE price > 200 ) Pro_above_200 PRO
INNER JOIN Purchases PUR
ON PUR.product_id = PRO.product_id ) T
GROUP BY customer_id, product_id
HAVING COUNT(1) = (SELECT COUNT(1) FROM Product WHERE price > 200 )
Try this:
SELECT distinct customer_id from purchase
where customer_id not in
(
select customer_id
from(
select purchases.customer_id ,
case when Product.proce>200 then 1 else 0 end as price_gt_twh
from purchases
left join Producton purchases.product_id =Product.product_id
)data
WHERE price_gt_twh=0
)customers
Select * from customer join select * from product where product.price > 200.
I'm assuming you have the price column in your product table
What you need is a join statement

MySQL error with union

What is wrong with the following sql query
select itemname from Item where itemid
in
((select ItemID
FROM Delivery NATURAL JOIN Supplier
WHERE SupplierName = 'Nepalese Corp.')
union
(select ItemID
FROM Sale NATURAL JOIN Department
WHERE DepartmentName = 'Navigation'))
I have seen another post on this site that recommends removing the inner parentheses on the two union sets and giving the first union set an alias. I tried it, and MYSQL shows an x at the line shown below, however, the query runs fine. My question is what happened?
select itemname from Item where itemid
in
(select ItemID as id
FROM Delivery NATURAL JOIN Supplier
WHERE SupplierName = 'Nepalese Corp.'
union
select ItemID . //shows an x at this line
FROM Sale NATURAL JOIN Department
WHERE DepartmentName = 'Navigation')
select itemname from Item
where itemid in
(
select ItemID
FROM Delivery
NATURAL JOIN Supplier WHERE SupplierName = 'Nepalese Corp.'
union
select ItemID
FROM Sale
NATURAL JOIN Department WHERE DepartmentName = 'Navigation'
)
You can try above code.
Simply remove unnecessary brackets will resolve you issue.
You can try join the inner result to main table
select itemname from Item as t1 join
(select distinct ItemID
FROM Delivery NATURAL JOIN Supplier
WHERE SupplierName = 'Nepalese Corp.'
union
select distinct ItemID
FROM Sale NATURAL JOIN Department
WHERE DepartmentName = 'Navigation') as t2 on t1.ItemID=t2.ItemID

MySQL - Matching ID to Max of Count ("Need" more elegant solution)

Is there a better way to do the following:
SELECT ProductID, MAX(a.countProductID)
FROM
(
SELECT ProductID, COUNT(ProductID) as countProductID
FROM SalesOrderDetail
LEFT JOIN Product USING (ProductID)
GROUP BY ProductID
) as a
WHERE a.countProductID = (SELECT MAX(x.countProductID) FROM
(
SELECT ProductID, COUNT(ProductID) as countProductID
FROM SalesOrderDetail
LEFT JOIN Product USING (ProductID)
GROUP BY ProductID
) as x
);
Since im using the same subquery twice. However i can't access the first one from the WHERE clause.
I guess the task is to find product or products with the maximum sales count. First you shouldn't join with PRODUCT table because all information you need is in SalesOrderDetail table. Then use LIMIT 1 to find maximum count and HAVING to select all products with maximum count:
SELECT ProductID, COUNT(ProductID) as countProductID
FROM SalesOrderDetail
GROUP BY ProductID
HAVING COUNT(ProductID) = (SELECT COUNT(ProductID) as countProductID
FROM SalesOrderDetail
GROUP BY ProductID
ORDER BY countProductID DESC
LIMIT 1 )
The final answer
SELECT ProductID, COUNT(ProductID) as countProductID
FROM SalesOrderDetail
LEFT JOIN Product USING (ProductID)
GROUP BY ProductID
ORDER BY countProductID desc
LIMIT 1

SQL query to return data from multiple tables?

I have tables with the following schemas:
items (itemID: integer, description: string, price: integer)
orders (orderID: integer, itemID: integer, aID: integer, customerID: integer, date: date)
I would like to find out each item for which there is only one order, return the itemID, description, and the date of the order.
I have the following code so far:
SELECT *
FROM (
SELECT *
FROM orders
GROUP BY itemID
HAVING COUNT(*) = 1
) AS ONLY_ONCE
But this returns only the information from the orders table (orderID, itemID, aID, and customerID)
in my code I search for items for which the itemID appears only once in the orders table, which means that it was only ordered once.
How do I get the description of these items which is in the items table?
I tried using the join function to do this but was not successful.
Thanks
You were very close... try this:
SELECT *
FROM items
WHERE itemID IN (
SELECT itemID
FROM orders
GROUP BY itemID
HAVING COUNT(*) = 1
)
If you also want the date from orders in your result set, there are many ways to do that, but I'd opt for a CTE and an inner join over the IN operator, like so:
;WITH SingleOrders AS (
SELECT itemID, MAX([date]) AS [Date]
FROM orders
GROUP BY itemID
HAVING COUNT(*) = 1
) SELECT *
FROM items i
INNER JOIN SingleOrders so ON i.itemID = so.itemID
The first part declares a common table expression called "SingleOrders" which consists of two columns: the ID and the Date for each itemID that has only one order. The MAX(date) returns the one and only date, and is required because of the GROUP BY clause.
The actual SELECT statement then joins this CTE with the items table to select out only the itemIDs that have just one order, and since it includes all the columns from both the items and the SingleOrders tables, will include the date.
If you don't want to use a CTE (or can't use one), something like this might work for you... just add an inner join back to the orders table to pick up the date:
SELECT i.*, o.date
FROM items i
INNER JOIN orders o ON i.itemID = o.itemID
WHERE i.itemID IN (
SELECT itemID
FROM orders
GROUP BY itemID
HAVING COUNT(*) = 1
)
You need to join your 'group' table result with the original data
SELECT I.*
FROM items I
INNER JOIN(
SELECT itemID
FROM orders
GROUP BY itemID HAVING COUNT(1)=1
) O ON O.itemID=I.itemID