2 criterias in the same query - mysql

i need some help...
In the same query can i have 2 different criteria?
Ex: I need to select, product type, product color and product price between 1000 to 2000.
In this research 10 products for example will be shown, and i would like to continue showing other products only with the price criterion.
The first 10 products are on the correct criteria and other products will be displayed with the price criterion.
Is this possible?
Thank you!

It's possible. You have to separate your criterias with AND :
SELECT * FROM ... WHERE product_type = 'something' AND product_color = 'something' AND product_rate > 1000 AND product_rate < 2000

Related

Total amount of sales done for each product using SQL

Here is the structure of 1st Table called Product.
PRODID PDESC PRICE CATEGORY DISCOUNT
101 BALL 10 SPORTS 5
102 SHIRT 20 APPAREL 10
Here is the structure of 2nd table called SaleDetail.
SALEID PRODID QUANTITY
1001 101 5
1001 101 2
1002 102 10
1002 102 5
I am trying to get total sales amount for each product by joining 2 tables. Here is the SQL i tried but its not giving correct result.
select a.prodid,
(sum((price - discount))),
sum(quantity),
(sum((price - discount))) * sum(quantity)
from product a
join saledetail b on a.prodid = b.prodid
group by a.prodid
2nd column of the query is giving incorrect final price. Please help me correct this SQL.
Please find an indicative answer to your question in the fiddle.
A problem stems from the aggregation of the difference of price. In case that the same product has two different prices, then these prices would be aggregated to one.
Moreover, you multiple the sums of the prices and quantities, while you need to perform the calculation on every sample. Look at the answer by #DanteTheSmith.
You might consider to use the SaleDetail table on the left side of your query.
SELECT SD.PRODID,
P.Price-P.Discount AS Final_Price,
SUM(SD.QUANTITY) AS Amount_Sold,
SUM((P.Price-P.Discount)*SD.QUANTITY) AS Sales_Amount
FROM SaleDetail AS SD
JOIN Product AS P
ON SD.PRODID = P.PRODID
GROUP BY SD.PRODID, P.Price-P.Discount
It would help if you built the example in SQL fiddle or gave the creates for the tables, but if I have to guess your problem is:
(sum((price - discount))) * sum(quantity)
needs to be:
sum((price - discount) * quantity)
(price - discount) * quantity is the function you wanna apply PER ROW of the joined table then you wanna add all those up with SUM() when grouping by prodid.
Furthermore, you can notice that (price - discount) needs to be done ONLY ONCE PER ROW so a quicker version would be to do:
(price-discount) * sum(quantity)
That would give you the total money earned for that product across all the sales you made, and I am guessing this is what you want?
I just notice you have a problem with 2nd column, dunno if that has been in question all along:
(sum((price - discount)))
Why are you summing? Do you want the money earned per product per unit of the product? Well guess what, your price is the same all the time, same as your discount so you can simply go with:
(price-discount) as PPP
NOTE: This assumes the discount is numerical (not percentage) and is applicable to all your sales, also the price is forever the same all which is not real life like.

multiply two columns from two different tables and group them together

I have two tables in MySQL which are sale and material details.
I want to calculate the profit I made by selling items which is
profit = (total) -(qty* landedcost)
Here is the structure of the two tables:
This is the query
SELECT sale.name ,sale.total-(sale.qty * materialdetails.landingcost) AS
result
FROM sale JOIN materialdetails
on sale.id = materialdetails.id
GROUP BY sale.name,result;
the result i get :
query result
I want something like this
name result
A4 5000
Computer 40000
Flash memory 1000
Headphone 22000
Mobile 35000
Any idea please?
You should sum the result and group by sale.name only, something like this:
SELECT sale.name ,sum(sale.total-(sale.qty * materialdetails.landingcost)) AS
result
FROM sale JOIN materialdetails
on sale.id = materialdetails.id
GROUP BY sale.name;
Explanation: if you group by two fields GROUP BY sale.name,result you will get one line for all records that have the same sale.name and result, so for instance
name result
Computer 10000
Computer 25000
are two different lines and they are not grouped together as one.

How can I get all products which have selected attributes in Prestashop

I am working on module development for Prestashop. Now I have situation in which I have to fetch all products which have selected attributes. There is a interface, where dropdown list of all active attributes are showing. And user does selects attributes as per need. Now, I want to find all the products on the basis of selected attributes.
Below are the table structure:
Product Table:
id_product id_shop ean upc quantity price
1 1 abc 50 16.99
2 1 def 25 25.99
Product Combination Table
id_attribute id_product
1 1
13 1
5 1
1 2
10 2
Can anyone please help on how can I fetch products on the basis of selected attributes??
Isn't this really basic My SQL ?
select * from product
where
(
select count(*) from
product_combination
where product_combination.id_attribute in (X,Y,Z)
and product.product_id = product_combination.product_id
) = 3
where X,Y,Z are the attributes the user selected, and 3 is the count of attributes selected.
If this is more involved, I think you need to edit your question to provide some more details as to exactly what the technical issue is. If its just that you dont know SQL then this isn't really the place to post your query.

SQL: Find closest number to given value with ties

I'm trying to find the closest number(s) to a given value in SQL. I have already made my query for multiple results:
SELECT *
FROM Cars
ORDER BY ABS(price - $price)
I know I can limit the table by using LIMIT 1, by which I have one number closest to the given value. But how can I include ties? Like for example when there are three or four cars with the same price? The amount of cars which have the same price is dynamic, so I can't specify a certain LIMIT.
I also know I could use SELECT TOP 1 WITH TIES, but I can't use this query because my database driver doesn't allow it. Does anybody have another idea of how to accomplish this?
Example:
car 1 = 2000
car 2 = 3000
car 3 = 3000
car 4 = 1500
When I want the cars closest to 3000, the query should return:
car 2
car 3
But without using a static LIMIT in the query, because the amount of cars with the same price can be different every time. Thanks
If your driver supports nested queries:
SELECT *
FROM CARS
WHERE ABS(price - $price) = ( SELECT MIN(ABS(price - $price)) FROM CARS )

Codeigniter mysql query joining 2 values across 3 tables

Although I can get the joins to work, it doesn't return the lowest deal possible. The result should be lowest handset price possible with the lowest monthly cost. Currently I can get the lowest handset price, but not with the lowest monthly cost and similarly lowest monthly cost but not with the lowest priced handset.
I have 3 tables DEALS, TARIFFS and HANDSETS. When a customer comes to a manufacturer product listing page, it loads all products related to that manufacturer (in most instances around 40 products). What I would like is it to load the cheapest deal available for each product. Tariff table has over 4000 records. Handset table has over 3000 records. Deals table has over 1 million records.
There is a representation of the the 3 tables below with relevant columns:
Handset Table
==================================
id | manufacturer_id
-----------------------------------
100 1
Deals Table
==================================================
tariff_id | handset_id | handset_cost
--------------------------------------------------
15 100 44.99
20 100 114.99
Tariffs Table
==============================
id | monthly_cost
------------------------------
15 12.50
20 7.50
This is the query
$this->db->select('h.name, t.monthly_cost, d.handset_cost');
$this->db->from('aff_deals d');
$this->db->join('aff_tariffs t', 't.id = d.tariff_id', 'inner');
$this->db->join('aff_handsets h', 'h.id = d.handset_id', 'inner');
if (isset($manuid)) {
$this->db->where('h.manufacturer_id', $manuid);
}
$this->db->order_by('d.handset_cost ASC');
$this->db->order_by('t.monthly_cost ASC');
$this->db->group_by('h.name');
$query = $this->db->get();
$result = $query->result();
Unfortunately this returns handset cost of £114.99 and monthly cost of £7.50, when I need £44.99 and £12.50. The example above is a simple snap shot. I have tried MIN(d.handset_cost), sub-queries but cannot get the desired results. Somehow I need to be able to get the lowest price handsets, even if it's 0.00 (FREE), with its equivalent monthly cost. Any help would be most welcome.
According to your query you are misusing Mysql's GROUP BY extension without having any aggregate function this will lead your query to return indeterminate results for columns which are absent in group by taking your query as an example,columns t.monthly_cost, d.handset_cost values are indeterminate if you are specific to pick minimum row from deals table per handset then you can use below query
SELECT h.name,
t.monthly_cost,
d.handset_cost
FROM aff_deals d
INNER JOIN (SELECT handset_id,MIN(handset_cost) handset_cost
FROM aff_deals
GROUP BY handset_id) dd
ON d.handset_id = dd.handset_id AND d.handset_cost = dd.handset_cost
INNER JOIN aff_tariffs t ON t.id = d.tariff_id
INNER JOIN aff_handsets h ON h.id = d.handset_id
WHERE h.manufacturer_id =1
ORDER BY d.handset_cost ASC,t.monthly_cost ASC
See Demo
For active record query it will difficult to replicate above (subselect) query bu you can directly run this query through $this->db->query('your query')