Select where rows exist more than once with variation - php

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.

Related

How to get price difference from same table in SQL

I have following table
slno date productid companyid price
88 2017-05-17 1 1 65.27
87 2017-05-17 1 2 72.94
86 2017-05-17 1 3 73.13
85 2017-05-17 2 1 73.73
84 2017-05-17 2 2 67.71
83 2017-05-16 1 1 65.40
82 2017-05-16 1 2 72.49
81 2017-05-16 2 1 73.31
80 2017-05-16 2 2 67.17
Now I want price of product 1 for 2017-05-17 and difference of price from yesterday for same company id.
e.g:
getPrice( productid = 1, date='2017-05-17')
and this should return :
companyid , productid , date, price, difference from yesterday:
1, 1,'2017-05-17', 65.27, -0.13
2, 1,'2017-05-17', 72.94, 0.45
...
or it should return:
companyid , productid , date, price, yesterday price:
1, 1,'2017-05-17', 65.27, 65.40
2, 1,'2017-05-17', 72.94, 72.49
...
How to get this in PHP SQL?
You should consider doing this with php, because php can tell you yesterdays and today date. So get yesterdays and today date with php and use it to manipulate the sql query
so the query should look something like this
yesterdays Price:
select companyid, productid , date, price from table where productid='1' and date='$yesterday';
Today price
select companyid, productid , date, price from table where productid='1' and date='$today';
now you can get both values and subtract them with php to get the difference

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 distinct manufacturer,count of 2 other columns and sum of value

Afternoon,
I have a couple of tables in mysql
The first holds the ticket information and status of ticket
The second holds the items and costs for each item.
Each ticket can have multiple items which are stored into the items table.
example table 1
Ticket id Manufacturer status
102 man-A 10
103 man-A 20
104 man-A 10
105 man-C 10
106 man-B 20
example table 2
Ticket id Item Price
102 item_a 100.00
103 item_a 100.00
103 item_b 110.00
103 item_c 115.00
104 item_c 115.00
105 item_b 110.00
106 item_a 100.00
106 item_c 115.00
now on a quick stats page i need to show.
Manufacturer Qty(status 10) Qty(status 20) Value
man-A 2 1 530.00
man-B 0 1 225.00
man-C 1 0 110.00
If there are no tickets with status 10 or status 20 i do not need to show that manufacturer.
I would like to do 1 mysql request to get the stats i need to show.
Many Thanks
Try this with join and using SUM()
SELECT
t1.Manufacturer,
SUM(t1.status =10) `Qty(status 10)`,
SUM(t1.status =20) `Qty(status 20)`,
SUM(t2.price) `Value`
FROM table1 t1
JOIN table2 t2 ON (t1.`Ticket id` =t1.`Ticket id`)
GROUP BY t1.Manufacturer
Fiddle Demo

MYSQL Update single records a table with MIN & MAX values from multiple records in 2nd table MYSQL

I have 2 tables houses & availability - I want to find the min and max for the RENT field for each HOUSEID in AVAILABILITY this contains many records per JOUSEID , then UPDATE the HOUSES table fields MIN_RENT and MAX_RENT, the HOUSES table contains only one entry per HOUSE ID
HOUSES
houseid min_rent max_rent
121 40.00 90.00
122 50.00 80.00
123 40.00 100.00
AVAILABILITY
house_id date rent
121 01/01/12 40
121 01/02/12 50
121 01/03/12 60
121 01/04/12 90
122 01/01/12 40
122 01/02/12 50
122 01/03/12 60
122 01/04/12 80
123 01/01/12 40
123 01/02/12 50
123 01/03/12 60
123 01/04/12 90
Regards
Martyn
a faster solution
UPDATE
HOUSES H
,(
SELECT
house_id
,MIN( rent ) AS min
,MAX( rent ) AS max
FROM
AVAILABILITY
GROUP BY
house_id
) AS A
SET
min_rent = A.min
,max_rent = A.max
WHERE
H.house_id = A.house_id

Multiple MySQL Queries and fatching MIN price from a table

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

Categories