How to get all the data from two tables? - mysql

I have two tables
1.Products
prod_id prod_name
1 honda
2 hero
3 marcedes
4 audi
2.Product to category
cat_id prod_id
1 1
1 2
2 3
2 4
Now i want result like below
prod_id prod_name cat_id
1 honda 1
2 hero 1
3 marcedes 2
4 audi 2
I cannot seem to figure this out. Any help greatly appreciated!

select products.*, category.cat_id
from products, category
where products.prod_id = category.prod_id

select products.prod_id ,products.prod_name, category.cat_id
from products, category
where category.prod_id = products.prod_id

select p.prod_id,p.prod_name,c.cat_id
from Products p inner join category c
on p.prod_id = c.prod_id

Simply use a join, if you only want products has a cat_id, then change LEFT JOIN to INNER JOIN.
SELECT t1.prod_id, t1.prod_name, t2.cat_id
FORM Products t1
LEFT JOIN ProductToCategory t2 ON t1.prod_id = t2.prod_id

select p.prod_id, p.prod_name,c.catid
from products p
inner join prodtocat pc on p.prod_id = pc.prod_id

Related

multiple mysql tables with multiple joins

I have 4 mysql tables as follows:
products:
----------------------------------------------------
product_id product_name price discount
----------------------------------------------------
1 product 1 10.00 1.00
2 product 2 20.00 2.00
3 product 3 25.00 1.00
----------------------------------------------------
subcategory
----------------------------------------------------
cb_category_id subcategory_name status
----------------------------------------------------
1 subcat 1 Enabled
2 subcat 2 Disabled
3 subcat 3 Enabled
------------------------------------------------------
temp_products
------------------------------------------------------
id productid catid
------------------------------------------------------
1 1 1
2 1 2
3 2 1
------------------------------------------------------
product_images
------------------------------------------------------
product_id images
------------------------------------------------------
1 image1.jpg
1 image2.jpg
2 image2-1.jpg
--------------------------------------------------------
temp_products.catid and subcategory.cb_category_id
and
temp_products.productid and products.product_id
and
products.product_id and product_images.product_id
are related..A product can have multiple images.
I wish to have a subcategory selected with all products with first image for a product from product_images and WHERE subcategory.status is "Enabled"...?? Need to limit output to only 1 cb_category_id with multiple product_id under it, like as follows:
----------------------------------------------------------------------------------
cb_category_id subcategory_name product_id product_name, price, discount, images
-----------------------------------------------------------------------------------
1 subcat 1 1 product 1 10.00 1.00 image1.jpg
1 subcat 1 2 product 2 20.00 2.00 image2- 1.jpg
My query is as follows:
SELECT p.product_id,p.product_name,p.price,p.discount,s.cb_category_id,s.subcategory_name
FROM products p,subcategory s
INNER JOIN temp_products ON p.product_id = temp_products.productid
INNER JOIN temp_products tp ON tp.catid = s.cb_category_id
WHERE tp.catid = s.cb_category_id
I am getting unknown column p.product_id in on clause ....Regarding including images i am at a dead end.
Help requested...I am unable to comprehend the joins required for the same...
you have a typo . you dont have products_id in your products table.
change this
p.products_id
to
p.product_id
EDIT: rewrite your query like that :
SELECT p.product_id,p.product_name,p.price,p.discount,s.cb_category_id,s.subcategory_name ,pi.images
FROM products p
INNER JOIN temp_products tp ON p.product_id = tp.productid
INNER JOIN product_images pi ON p.product_id = pi.product_id
INNER JOIN subcategory s ON tp.catid = s.cb_category_id
GROUP BY p.product_id
Your 'temp_products' table is basically a mapping table for 'products' and 'subcategory'. I have not tested the following code but you'll get the idea from comments(text after # character). this should solve your problem:
SELECT p.product_id,p.product_name,p.price,p.discount,s.cb_category_id,s.subcategory_name,pim.images
FROM products p
INNER JOIN temp_products tp ON p.product_id = tp.productid #remove duplicate join
INNER JOIN subcategory s ON tp.catid = s.cb_category_id
LEFT JOIN product_images pim ON pim.product_id =p.product_id #join for images
WHERE s.status = 'Enabled' AND #subcategory is enabled
s.cb_category_id = '1' #subcategory you want to be selected
GROUP BY p.product_id ORDER BY p.product_id, pim.images;

Select sold and unsold products

I'm trying to select all products names and how many has been sold in current date, but I'm having a problem since not all products are sold everyday. (When the product has not been sold it must return 0)
TABLE PRODUCTS
ID NAME
1 APPLE
2 PINEAPPLE
3 COFFE
TABLE SALES
ID DATE
1 2014-01-13
2 2014-01-13
TABLE PRODUCTS_AND_SALES
SALE_ID PRODUCT_ID AMOUNT
1 3 2
1 1 1
2 3 1
What I expect to receive:
PRODUCT AMOUNT
APPLE 1
PINEAPPLE 0
COFFE 3
What I receive:
PRODUCT AMOUNT
APPLE 1
COFFE 3
My query:
select product, sum(amount) from products
join products_and_sales using (product_id)
join sales using (sale_id)
where date(dt_sale) = curdate()
group by product_id;
try this
select name as PRODUCT , ifnull(sum(AMOUNT),0) amount from products p
left join PRODUCTS_AND_SALES ps
on p.id = ps.PRODUCT_ID
group by product
DEMO HERE
EDIT:
if you wanna use specefic date then use this
select name as PRODUCT ,if(date = '2014-01-13' ,ifnull(sum(AMOUNT),0), 0 ) amount
from products p
left join PRODUCTS_AND_SALES ps on p.id = ps.PRODUCT_ID
left join SALES s on s.id = ps.SALE_ID
group by product
DEMO HERE
just replace this date 2014-01-13 by the date you want. (curdate()) or what ever
Use OUTER JOIN instead of INNER JOIN
if you show the query you use we will correct it for you.
Try this...
SELECT pr.NAME, IFNULL(SUM(prs.AMOUNT), 0)
FROM PRODUCTS pr
LEFT OUTER JOIN PRODUCTS_AND_SALES prs ON pr.id = prs.product_id
LEFT OUTER JOIN SALES sl ON sl.id = prs.sales_id AND date(s1.dt_sale) = curdate()
GROUP BY pr.NAME

Join two table. One column using SUM

I need a little help with a MySQL query.
I have two tables one table is a list of product and one table is a list of warehouses quantity
product:
product_id product_name
1 name1
2 name2
3 name3
warehouse_product
id warehouse_id product_id product_quantity
1 1 1 15
2 2 1 30
3 1 2 100
4 2 2 30
5 1 3 20
6 2 3 40
The results Im looking to get from the above data would be
product_id product_name product_quantity
1 name1 45
2 name2 130
3 name3 60
I've tried many query but it's not working. My query is:
SELECT
product_id as product_id, SUM(quantity) as quantity
FROM
(SELECT
p.product_id as product_id, wp.product_quantity as quantity
FROM
product as p
LEFT JOIN warehouse_product as wp ON p.product_id = wp.product_id
WHERE
product_active = 1)
Try this:
SELECT p.product_id, p.product_name, SUM(wp.quantity) as quantity
FROM product as p
LEFT JOIN warehouse_product as wp
ON p.product_id = wp.product_id
AND product_active = 1
GROUP BY p.product_id, p.product_name
After JOINing the two tables you need to use GROUP BY so that SUM of quantity can be calculated for each product.
select p.product_id,p.product_name,sum(product_quantity) as 'product_quantity'
from product p inner join warehouse_product whp on
whp.product_id=p.product_id group by whp.product_id
select p.product_id, p.product_name,sum(w.product_quantity)
from
product p
inner join
warehouse_product w
on
p.product_id = w.product_id
group by w.product_id
order by p.product_name;
SELECT P.productid,
P.ProductName,
Sum(W.product_quantity) As quantity
FROM Product AS P
LEFT JOIN warehouse_product AS W
ON P.productid = W.productid
GROUP BY P.ProductId , P.ProductName
SQLFiddle

Select only the latest version of the data with SQL join

I have two tables one containg offer information and the other containg products
something like this:
OFFER PRODUCTS
ID Number Version poID offer_id Product how_many
========================== ========================================
1 123 1 1 1 Apple 1
2 123 2 2 1 Banana 2
3 124 1 3 1 Orange 1
4 2 Apple 1
5 2 Banana 2
6 2 Orange 2
7 2 Kiwi 1
8 3 Apple 2
9 3 Banana 3
I would like a list of how many products that are currently offered.
Since OFFER(id = 2) is an update of (id = 1) only (id = 2) should be counted.
How should I best query this?
First you need to get all the latests offers:
select o.id
from offer o
where version = (select max(version)
from offer o2
where o2.number = o.number);
Based on the above you can then get all the products:
select p.*
from products p
where offer_id in (select o.id
from offer o
where version = (select max(version)
from offer o2
where o2.number = o.number));
If id and version correlate:
select sum(how_many) from products p
join offer on p.offer_id=offer.id
join (
select number, max(version) version from offer group by number
) x
on offer.id=x.id and offer.version = x.version
SELECT *
FROM products
WHERE offer_id = (SELECT MAX(id) FROM offer)
or, if you prefer the join syntax
SELECT p.*
FROM products p
INNER JOIN (SELECT MAX(id) id FROM offer) o ON p.offer_id = o.id
Edit (still not completely sure this is what you want without seeing your desired results)
SELECT p.*
FROM products p
INNER JOIN offer o on p.offer_id = o.id
INNER JOIN
(SELECT number, max(version)
FROM offer
GROUP BY number
) oMax ON o.number = oMax.number AND o.version = oMax.version
Try this:
select [list columns here]
from products p
join (select offernumber, max(id) as ID from offer group by offernumber) a
on a.id = p.offer_id
If you need addtional columns from offer other than the offernumber and the id:
select [list columns here]
from products p
join (select offernumber, max(id) as ID from offer group by offernumber) a
on a.id = p.offer_id
join offer o on o.id = a.id

mysql count, distinct, join? Confusion

i have 2 tables:
tblItems
ID | orderID | productID
1 1 2
2 1 2
3 2 1
4 3 2
tblProducts
productID | productName
1 ABC
2 DEF
im attempting to find the most popular Product based on whats in "tblItems", and display the product Name and the number of times it appears in the tblItems table.
i can get mysql to count up the total like:
$sql="SELECT COUNT(productID) AS CountProductID FROM tblItems";
but i can't figure out how to join the products table on..if i try LEFT JOIN the query goes horribly wrong
hopefully thats not too confusing..thankss
Are you simply trying to find the count of orders by product like so:
Select P.ProductName, Count(*)
From tblItems As I
Join tblProducts As P
On P.ProductId = I.ProductId
Group By P.ProductName
I think you may be looking for something like:
SELECT tblProducts.productName, COUNT(tblItems.ID)
FROM tblProducts
LEFT JOIN tblItems USING(productID)
GROUP BY tblProducts.productID
SELECT count(i.productID) AS cnt, p.productName FROM tblItems AS i
LEFT JOIN tblProducts AS p ON p.productID = i.productID
GROUP BY i.productID
ORDER BY cnt desc