Product Table columns:
image1
product_type
product.amount
price
description
product_id
Offers Table columns:
Offer_id
Product_id
Offered person
I need to get count of offer_id by using product_id on these two tables. I also need to get product list my filtering by product_type.
I have tried the following:
SELECT COUNT(offers_id) AS offers,
image1,
product_type,
product.amount,
product.price,
description,
product.product_id
FROM product INNER JOIN offers ON product.product_id = offers.product_id
WHERE product_type LIKE '%$product_type%'"
But this query returns only one product from the product table.
If you want a count of grouped information you have to use GROUP BY, e.g:
SELECT COUNT(offers_id) AS offers, image1, product_type, product.amount, product.price, description, product.product_id
FROM product INNER JOIN offers ON product.product_id = offers.product_id
WHERE product_type LIKE '%$product_type%'"
GROUP BY image1, product_type, product.amount, product.price, description, product.product_id
Related
Here,in billtran table the productid=1 is repeated twice
I want to find the sum(billtran.quantity) of each productid separately
select Query1:
select name,billtran.quantity from product inner join billtran on product.id=billtran.productid where product.id in(select id from product)
Per comment:
select name, SUM(billtran.quantity) from product inner join billtran on product.id=billtran.productid where product.id in(select id from product)
GROUP BY NAME
My additions in caps
Note: where product.id in(select id from product) is an entirely useless where clause and should be removed. Queries don't have to have a where clause and don't need one that says a table id should be in all the ids in that table (always true)
I would hence have written this one as:
SELECT
p.name,
SUM(b.quantity) as sumof_quantity
FROM
product p
INNER JOIN billtran b ON p.id=b.productid
GROUP BY
p.name
Very simple query. Pls check below:
select product.name,sum(billtran.quantity) from product inner join billtran on product.id=billtran.productid group by billtran.productid
First of all you have to get the sum of the quantity according to the product_id
SELECT product.name, SUM(billtran.quantity) AS value_sum
FROM billtran inner join product on product.id= billtran.product_id where product.id in(select id from product )
GROUP BY product_id;
This will be the result:
name quantity
abc 6
xyz 1
pqt 3
You can get the same result by grouping the product.name as well:
SELECT product.name, SUM(billtran.quantity) AS value_sum
FROM billtran inner join product on product.id=billtran.product_id where product.id in(select id from product)
GROUP BY product.name;
I want to get product by category filter and order by category count belong to product.
Two table - product and pro_cat
customer filter the category id 10 and 15 than product list be order by number of category belongs to each product. For ex. Product id 1 belong to 4 categories so it will come first that so on.
SELECT product.id
, product.name
FROM product
JOIN pro_cat
ON product.id = pro_cat.product_id
where category_id in (10,15)
group
by product.name;
do the join between the two table you could use count(*)
SELECT product.id
, product.name
FROM product
JOIN pro_cat ON product.id = pro_cat.product_id
where category_id in (10,15)
group by product.id;
order by count(*) DESC
I have the following three tables: Product, User and Purchased.
Product contains:
A unique identifier (productID)
A name (productname)
A price
(productprice)
User contains:
A unique identifier (userID)
Purchased contains:
Two identifiers noted as private keys, productID and userID
A date of the record (creationdate)
My query should return a list of unique products that were bought on the retailer’s site since January 1st with most expensive product returned first.
My Query:
SELECT Product.productID, Product.productname, Purchased.creationdate
FROM Product
INNER JOIN Purchased
ON Product.productID = Purchased.productID;
ORDER BY Product.productprice DESC;
If you just want a list of products, I would suggest exists rather than join:
select p.*
from products p
where exists (select 1
from purchased pu
where pu.productId = p.productId and
year(pu.creationdate) = year(now())
)
order by price desc;
Hi I want to get opposite of intersect from two tables.
I have a sale table and purchase table. What I want to do is get all purchases ids where not included in the sales table.
sale table
sale_id (pk)
product_id (fk)
purchase_id (fk)
purchase table
product_id (fk)
purchase_id (pk)
SELECT DISTINCT purchase_id
, product_id
FROM
purchase
INNER JOIN sale
USING (purchase_id, product_id);
Here is an example:
If I run the above code, this will be the result.
purchase_id product id
1 1
1 2
1 4
2 1
2 3
Now I want to get:
purchase_id product id
1 3
2 2
In short I want to get inverse of above code. Thanks in advance.
Okay, I think I understand better now.
This should return any entry in purchase that have no matching entry in sales.
SELECT
`purchase`.`purchase_id`, `purchase`.`product_id`
FROM `purchase`
LEFT JOIN `sale` ON `sale`.`purchase_id` = `purchase`.`purchase_id` AND `sale`.`product_id` = `purchase`.`product_id`
WHERE
`sale`.`sale_id` IS NULL
ORDER BY
`purchase`.`purchase_id`, `purchase`.`product_id`
If you want to get all the purchases that have no related values in the sales table, you can use a LEFT JOIN:
select
p.purchase_id
from
purchase as p
left join sale as s on p.purchase_id = s.purchase_id
where
s.purchase_id is null;
"Unilateral" joins (LEFT JOIN, RIGHT JOIN) are useful when you want to get data from a table even if data in another related table does not exist. Of course, that means that you can filter data from one table when there's no related data in a second table.
Hope this helps.
Looking at your updated question and your comment, I think that you want all the possible combinations not used.
You'll need to split this in two steps:
First you need all the possible combinations of purchase_id and sale_id values (the "cartesian product" of both the sets).
Then you need to get all the combinations already used.
Finally you need to exclude all the combinations already used.
This can be done using subqueries.
Step 1.
select distinct p.purchase_id, s.product_id from purchase as p, sale as s;
Step 2. (Your query)
select distinct
purchase_id, product_id
from
purchase as p
inner join sale as s
on (p.purchase_id = s.purchase_id and p.product_id = s.product_id);
Step 3. Put it all together
select
a.*
from
(select distinct p.purchase_id, s.product_id from purchase as p, sale as s) as a
left join (
select distinct
purchase_id, product_id
from
purchase as p
inner join sale as s
on (p.purchase_id = s.purchase_id and p.product_id = s.product_id)
) as e on (a.purchase_id = e.purchase_id and a.product_id = e.product_id)
where
e.purchase_id is null and e.product_id is null;
I have a product table, with following fields
productid
name
price
catid
deleted (can be any one from 'y' and 'n')
and there is a sales table
salesid
productid
salestime
paymentstatus (enum, can have any one value: COMPLETED, REMAINING, CANCELLED)
Now I need data in following format
catid productids totalproducts totalsales
where
productids: this will list all the product id (comma seperated) where deleted = 'n' and belong to particular category
totalproducts: products belonging to particular category, where deleted = 'n'
totalsales: sales, where product belong to particular category and paymentstatus = 'COMPLETED'
any help.
With this query, you will get the total sales of a particlar category where the product is deleted.
Did you want this or did you want ALL the sales of a category, no matter the state of the product ?
SELECT p.catid AS catid,
GROUP_CONCAT(DISTINCT p.productid SEPARATOR ',') AS productids,
COUNT(DISTINCT p.productid) AS totalproducts,
COUNT(s.salesid) AS totalsales
FROM products p
LEFT OUTER JOIN sales s
ON s.productid = p.productid
AND s.paymentstatus = 'COMPLETED'
WHERE p.deleted = 'n'
GROUP BY p.catid