Mysql selecting some field according to some condition - php

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)

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 Skip columns on condition

I have the following table:
id speed date
1 0 01/01/2015
2 0 01/01/2015
3 0 01/01/2015
4 0 01/01/2015
5 0 01/01/2015
6 0 01/01/2015
7 25 01/01/2015
8 78 01/01/2015
9 13 01/01/2015
10 45 01/01/2015
11 0 01/01/2015
12 80 01/01/2015
13 86 01/01/2015
14 10 01/01/2015
15 0 01/01/2015
16 0 01/01/2015
17 0 01/01/2015
18 0 01/01/2015
This is just a small part of the table, it has many dates. Each new date starts and ends with an unknown amount of zeros. And I have to skip these zeros and get only what's between them. So I came up with the following solution:
Get the ID of the first row whose speed is positive (in our table it's id number 4).
Get the ID of the last row whose speed is positive (in our table it's id number 12).
Get lines between these two IDs (4 and 12).
I get the id of the first row whose speed is positive:
$q1=$conn->prepare("SELECT * FROM tableX WHERE speed > 0 order by date asc LIMIT 1");
$q1->execute();
$r1=$q1->fetch();
$first_id = $r1['id'];
And then get the id of the last row whose speed is positive:
$q2=$conn->prepare("SELECT * FROM tableX WHERE speed > 0 order by date desc LIMIT 1");
$q2->execute();
$r2=$q2->fetch();
$last_id = $r2['id'];
And then use those ids to get what I want:
$q3=$conn->prepare("SELECT * FROM tableX WHERE id between '$first_id' and '$last_id'");
$q3->execute();
while($r3=$q3->fetch(){}
Expected result:
id speed date
7 25 01/01/2015
8 78 01/01/2015
9 13 01/01/2015
10 45 01/01/2015
11 0 01/01/2015
12 80 01/01/2015
13 86 01/01/2015
14 10 01/01/2015
PS: row 11 is not a bug.
My code is actually working but I think this solution of mine is lame, so I am looking for an improvement, kind of all these three queries in one!
I'm not sure if this is exactly what you want, but I think this will be equivalent to your whole code:
$q3=$conn->prepare("SELECT * FROM tableX WHERE speed > 0");
$q3->execute();
while($r3=$q3->fetch(){}

How to calculate MLM User Income points in php

I'm trying to Calculate Income Points for MLM network Users (i.e).,
I'm having five types of kits such as Rs.0/-, Rs.1000/-, Rs.2000/-, Rs.4000/-, Rs.10000/- .
Once the kit is purchased a unique pin and user id will generate for each individual kit like
If 2 number of 2000/- kit purchased means userid and pin will be
2000100 539fdda37c435 and
2000101 5395b0d8b66d1 .....
Then Based on new user sponsor id(Sponsor id will be old user), registration will be proceed with Group left and right.
Now what's my problem is While calculating income point of a user how can i take their children user from top to bottom. I'd strucked in MYSQL Query.
For Example.,
now how can i get each node's value for a parent node(1) to root node(12) from this dynamic tree in php ??
I struck in this loop concept.,
Thanks IN Advance.,
database architecture
id par lev lef rgt status wallet userstatus
1 1 1 2 3 1 0 0
2 2 2 4 5 1 0 0
6 3 2 6 7 1 0 0
8 4 3 8 9 1 0 0
9 5 3 10 11 1 0 0
10 6 3 12 13 1 0 0
11 7 3 0 0 1 0 0

Optimal way to pass variable with combination of properties like unix permissions

Apologise for a vague title.
What is the best way to pass a finite number of properties(e.g. read, write, execute, ...). I think I could do this like these permissions - count the sum of some numbers assigned to properties - 1,2,4,8,16... Is it the best easy to do this and what algorithm should I use to get the summands - for example if I have 17 - how could I calculate that this is 16 and 1?
Thanks.
You could use Bitwise Operators. This way you can compare the actual bits of an integer, Here's how it works:
Bit Pattern
8 4 2 1
1 0 0 0 = 8
0 1 1 1 = 7
0 1 1 0 = 6
0 1 0 1 = 5
0 1 0 0 = 4
0 0 1 1 = 3
0 0 1 0 = 2
0 0 0 1 = 1
0 0 0 0 = 0
So,
6 & 4 = 4
Means show me the bits that are present in both 6 and 4.
Compare '0 1 1 0' and '0 1 0 0' and you will see that the second bit '4' is the only bit populated in both, so return 4.
Or
7 & 3 = 3
Means show me the bits that are present in both 7 and 3.
Compare '0 1 1 1' and '0 0 1 1' and you will see that the third (2) and fourth (1) bits are populated in both, so return 3 (2 + 1).

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