Multiple MySQL Queries and fatching MIN price from a table - php

TABLE PRODUCTS_CATEGORIES
product_id category_id link_type position
2 22 M 0
3 22 M 0
4 22 M 0
5 22 M 0
6 1 M 0
7 1 M 0
8 1 M 0
9 1 M 0
10 1 M 0
11 1 M 0
TABLE PRODUCT_PRICES
product_id price lower_limit usergroup_id
2 39.99 1 0
3 69.99 1 0
4 99.99 1 0
5 124.99 1 0
6 169.99 1 0
7 199.99 1 0
8 249.99 1 0
9 24.99 1 0
10 29.99 1 0
11 34.99 1 0
I want to be able to grab the lowest products price from the category - the function i have currently made is:
function fn_get_category_min_price($category_id)
{
if (!empty($category_id))
{
$product_min_price = db_get_field("SELECT product_id FROM ?:products_categories WHERE category_id = ?i", $category_id);
$category_min_price = db_get_field("SELECT MIN(price) FROM ?:product_prices WHERE product_id = ?i", $product_min_price);
if (!empty($category_min_price))
{
return $category_min_price;
}
else
{
return "";
}
}
return false;
}
but it is not 100% working, although it grabs the price from the right category_id - it does not appear to be grabbing the lowest all the time.... anyone have any ideas or a better way of writing those mysql queries??

As far as I understand, your code takes first product from specified category and then just returns it's price. Sometimes it's minimum, sometimes not.
This can be done with single SQL query:
select min(price) from product_prices p, product_categories c where p.product_id=c.product_id and c.category_id = $category_id

You can do this with an single SQL query by joining the two tables (Natural join):
Select Min(`prize`)
from product_categories natural join product_prices
where category_id = ?i

Related

Select where rows exist more than once with variation

This is a hard one to put a title to so I apologise for ambiguity.
I have the following MySQL table (it's a Magento table):
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
3 45 1 175 Sale Outerwear
4 45 0 177 (null)
5 45 1 177 New Arrivals
6 45 0 178 New Tops
7 45 1 178 New Tops
As you can see, some of the rows have the same everything except store_id.
I want to do the following:
If a row with store_id = 0 has a duplicate row, but with store_id = 1 and different values (for example, rows 4 and 5 above), update the row with store_id = 0 to have the same value as the other.
Delete the row with store_id = 1
I know I will probably need a combination of both PHP and MySQL for this. I just don't know what the MySQL query would be.
Any help would be great!
EDIT
The end goal from the above table is the following
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
4 45 0 177 New Arrivals
6 45 0 178 New Tops
In order to retrive redundunt values having the same entity_id, you can do :
SELECT
*
FROM
magento m1, magento m2
WHERE
m1.attr_id=m2.attr_id
AND
m1.entity_id=m2.entity_id
AND
m1.id > m2.id
And for fixing null values, you will need to loop the above results and search for the null and replace it with the previous or next result.

Mysql selecting some field according to some condition

id price tax item discount
1 500 15 100 0
2 0 15 200 1
3 100 15 500 0
4 0 15 300 0
5 1000 15 88 0
6 0 15 190 1
7 0 15 120 0
Select All if both price and discount or not equal to zero.
I don't want to select if the value of price and discount both are zero in mysql
Not to select only if price and isDiscount both are zero... (only one case)
Price IsDiscount Select
1 1 Yes
!0 0 yes
0 1 Yes
0 0 no
I might be missing something here, ...but isn't it as simple as
SELECT
id,tax,...
FROM
tablename
WHERE
price!=0
OR discount!=0
?
Here is your simple answer.
the query is as follows :
select * from orders WHERE price+discount > 0
select * from from_your_table WHERE price+discount > 0
SELECT
id,tax,...
FROM
tablename
WHERE
(price >0) or (discount>0) or (discount>0 and price>0)

mysql update sequence into one table depending on another table

Below are the 2 tables:
classes
academic_year_id student_id standard
2 1 10
2 2 10
2 3 10
2 4 10
2 5 10
2 6 10
2 7 11 Science
2 8 11 Science
Here standard is not INT
student
student_id roll_no name teacher_approval
1 0 S Sumeet G 1
2 0 Nair Nikhil R 1
3 0 Nayak Ankita R 0
4 0 Rathod Ketan P 0
5 0 Patel Vishal D 1
6 0 Patel Jignesh R 0
7 0 Prajapati Bhavesh A 1
8 0 Shah Harsh N 1
What i want to do:
when teacher selects standard -> 10 and press a button "Assign Roll No"
i want all student of 10th standard to be given roll no sequentially order by name and which are approved by teacher that is teacher_approval = '1'
So my student table becomes as below:
student
student_id roll_no name teacher_approval
1 3 S Sumeet G 1
2 1 Nair Nikhil R 1
3 0 Nayak Ankita R 0
4 0 Rathod Ketan P 0
5 2 Patel Vishal D 1
6 0 Patel Jignesh R 0
7 0 Prajapati Bhavesh A 1
8 0 Shah Harsh N 1
I have so far tried below code:
$standard = $_POST['standard']
SET #incr = 0
UPDATE
student AS st
JOIN
( SELECT * FROM
classes AS classes
WHERE standard = '".$standard."'
) AS tmp
ON tmp.student_id = st.id
SET
st.roll_no = #incr:=#incr+1
WHERE st.teacher_approval = '1'
ORDER BY st.name ASC
But it gives me an error: incorrect usage of update and order by
Anyone with a similar experience? Any help would be appreciated. Thanks.
In MySql, you can't have an ORDER BY as part of the UPDATE directly when using multiple tables (see this link). Try this instead :
UPDATE student AS table1
INNER JOIN (
SELECT st.student_id, st.roll_no, st.name, st.teacher_approval
FROM student AS st
JOIN (
SELECT * FROM
classes AS cl
WHERE cl.standard = '".$standard."'
) AS tmp
ON tmp.student_id = st.student_id
WHERE st.teacher_approval = '1'
ORDER BY st.name ASC
) AS table2
ON table2.student_id = table1.student_id
SET table1.roll_no = #incr:=#incr+1

SORT a specific value in mysql_fetch_array

I have data set with full of data...
Id | loc |SubLoc |beds
1 15 5803 1
2 15 5803 1
3 16 2813 1
4 16 2813 2
5 16 2830 1
6 20 2020 4
7 19 2513 3
I have this php/mysql code to traverse it
select id,loc,SubLoc,beds from table where id != 144 AND price > 140000 AND price < 210000 AND category_id=1
while($rw = mysql_fetch_array($rs2)){}
Now i want to sort $rw in this manner
I have three values
loc=16,SubLoc=2813,beds=1
I want to show same result but show all results in this manner
1- having loc==16 on top then
2- having SubLoc = 2813 then
3- having beds = 1
and then rest of results
How this can be done in php?
UPDATED
I want to get these Ids
3 as it matches 3 parameters
4 as it matches 2 parameters
5 as it matches 2 parametrs
1 as it matches 1 parameter
and so on
Try that:
select id,loc,SubLoc,beds from table1
where loc = 16 or subloc = 2813 or beds =1
order by
case when loc = 16 then 0
when subloc = 2813 then 1
when beds = 1 then 2 end asc
DEMO
to get other results also without matching then use this:
select id,loc,SubLoc,beds from table1
order by
case when loc = 16 then 0
when subloc = 2813 then 1
when beds = 1 then 2
else 3 end asc
DEMO
EDIT: to include all cases use this:
select id,loc,SubLoc,beds from table1
order by
case when loc = 16 and subloc=2013 and beds = 1 then 0
when loc = 16 and subloc = 2813 then 1
when loc = 16 and beds = 1 then 2
when subloc = 2813 and beds = 1 then 3
when loc = 16 then 4
when subloc = 2813 then 5
when beds = 1 then 6
else 7 end asc
Try with order
ORDER BY SubLoc=16 DESC,loc, beds

PHP sql multiple attributes

I have the folowing query that shows my products.
SELECT DISTINCT productsmap.id, attributes_product.waarde,
attributes_product.att_id, productsmap.name, productsmap.category
AS categoryId, brand.naam AS brand, productsmap.sku, categorie.name AS
category, productsmap.price, productsmap.shops
FROM productsmap
INNER JOIN categorie ON productsmap.category = categorie.id
INNER JOIN brand ON productsmap.brand = brand.id
INNER JOIN attributes_product ON productsmap.id = attributes_product.pmapid
WHERE productsmap.category
IN ( 2, 3, 4, 5, 6, 7, 8 )
AND productsmap.shops >0
AND (
productsmap.price >= 3.94
AND productsmap.price <= 204.99
)
AND productsmap.online =1
LIMIT 0 , 30
It gives the folowing results I cropped a bit:
(id) (waarde) (att_id) (name) (categoryId) (brand) (sku)
2 109 1 Name 4 Merk 70000
2 2013 2 Name etc etc etc
2 123 3 Name etc etc etc
3 103 1 Name2 etc etc
3 2012 2 Name2
3 123 3 Name2
4 110 1 3name
4 2013 2 3name
4 102 3 3name
Whit multiple times the same id and name only diffrent att_id and waarde. But i need to add to my query (attributes_product.att_id = 1 and waarde = 109) and (attributes_product.att_id = 2 and waarde = 2013)
But this is not possible because it are diffrent results. How can I solve this issue? Or on what can i search to solve this issue?
It attributes.product are att_id and waarde

Categories