SELECT not returning anything using HAVING clause - mysql

I'm trying to get the total percentage off and only return the matches >+ 80. However, this doesn't return any results:
SELECT * FROM products WHERE Available=1 AND Merchant='Amazon' HAVING (LowestUsedPrice - LowestNewPrice) / LowestNewPrice * 100 >= ?
Am I using HAVING correctly?

HAVING specifies a search condition for a group or an aggregate function used in SELECT statement.
HAVING is applied after the aggregation phase and must be used if you want to filter aggregate results.
Your query is wrong.
What you can do is do the conditioning in where clause only.
SELECT *
FROM products
WHERE Available=1
AND Merchant='Amazon'
AND (LowestUsedPrice - LowestNewPrice) / LowestNewPrice * 100 >= ?

As per I understand you wanna use having to filter whereas you may use just where condition.
About returning results, your query will produce syntax error. If you use as following and don't get any result then obviously it is because of conditions and your data. In that case if you provide data, you may get some help.
SELECT * FROM products
WHERE Available=1
AND Merchant='Amazon'
AND (LowestUsedPrice - LowestNewPrice) / LowestNewPrice * 100 >= ?

SELECT *, (LowestUsedPrice - LowestNewPrice) / LowestNewPrice * 100 as percentage FROM products WHERE Available=1 AND Merchant='Amazon' HAVING percentage >= ?

Your main problem is that your percentage formula is wrong. Use
((LowestUsedPrice - LowestNewPrice) / LowestNewPrice) * 100
or
(LowestUsedPrice - LowestNewPrice) * 100 / LowestNewPrice
The way you are using will do something like
(20 - 8) / 15 * 100
12 / 1500 = 0.008
And you need
1200 / 15 = 80
Add this fix to the solution on other answers

Related

My sql query with MAX, MIN and third field for HAVING

I'm trying to get the minimum and maximum price on a mysql(MyISAM) query.
I'm using this query for:
SELECT MAX(price_feed) as max,
MIN(price_feed) as min,
SQRT( POW(69.1 * (latitude_feed - 51.542980), 2) + POW(69.1 * (-0.149323 - longitude_feed ) * COS(latitude_feed / 57.3), 2)) AS distance
FROM feed
WHERE listing_type_feed = 'rental'
and property_type_feed IN ("Flat", "Apartament", "Penthouse", "Studio")
HAVING distance < 2
but it returns nothing, while when i try
SELECT price_feed as max,
price_feed as min,
SQRT( POW(69.1 * (latitude_feed - 51.542980), 2) + POW(69.1 * (-0.149323 - longitude_feed ) * COS(latitude_feed / 57.3), 2)) AS distance
FROM feed
WHERE listing_type_feed = 'rental'
and property_type_feed IN ("Flat", "Apartament", "Penthouse", "Studio")
HAVING distance < 2
It returns 2600 rows.
Thanks
You need a nested query or CTE depending on your RDBMS
First you calculate what property are in a 2km radius and then you calculate the max/min prices from that result, also you dont need having instead you use the whereclausule
SELECT MAX(price_feed) as max, MIN(price_feed) as min
FROM (
SELECT price_feed
FROM feed
WHERE
listing_type_feed = 'rental'
and property_type_feed IN ("Flat", "Apartament", "Penthouse", "Studio")
and SQRT( POW(69.1 * (latitude_feed - 51.542980), 2) + POW(69.1 * (-0.149323 - longitude_feed ) * COS(latitude_feed / 57.3), 2)) < 2
) as filter_properties
In pure SQL it is a mistake to select fields that are not aggregate functions nor group by fields, in a query that uses aggregates or group by.
In mysql you can select any field, but if it is not a group by field or an aggregate function the value returned may be any value from the resultset. So it is undefined unless you group by a key.
In your first query there is no group by clause, so the aggregates use all rows as one group. And the value of distance is the value of a row (any).

Get difference between two numbers in WHERE statement?

I'm trying to get the difference between two numbers (one of the values in a column and one arbitrary one) and then check if the difference is less than .20 in a WHERE statement. Here is what I have so far:
SELECT * FROM `products` WHERE (`lowest_price` - 5) >= .20 OR (5 - `lowest_price`) <= .20
(where 5 is the arbitrary number, and lowest_price is the column I'm comparing it to)
However, when I run this statement, I get results that I'm not expecting. What am I doing wrong?
The SQL statement is wrong
Try SELECT * FROM products WHERE (lowest_price - 5.0) BETWEEN -0.2 and 0.2
You could try SELECT (lowest_price - 5.0), * FROM products WHERE (lowest_price - 5.0) BETWEEN -0.2 and 0.2 which will help you where you are going wrong.

mysql get difference from the result of 2 fields queried

I'm not good at sql but I can create,understand common SQL queries. While scouring the net it seems its hard to find a befitting way on this query.
I have a query which is
SELECT COUNT(`BetID`),
FORMAT(SUM(`BetAmount`),0),
FORMAT(SUM(`Payout`),0),
ROUND((SUM(`BetAmount`) / COUNT(`BetID`)),2),
ROUND((((SUM(`BetAmount`) + SUM(`Payout`)) / SUM(`Payout`)) * 100),2)
FROM `betdb`
I would like to subtract the result of
FORMAT(SUM(`BetAmount`),0)
and
FORMAT(SUM(`Payout`),0)
Any other ideas to execute subtraction in this mysql query?
If you want the numbers rounded before subtracting them (which seems to be the case when you want to subtract the formatted numbers), you'll need to round them first to the same precision as the formatting, subtract and lastly format the result;
SELECT COUNT(`BetID`),
FORMAT(SUM(`BetAmount`),0),
FORMAT(SUM(`Payout`),0),
FORMAT(ROUND(SUM(`BetAmount`),0) - ROUND(SUM(`Payout`),0),0) diff,
ROUND((SUM(`BetAmount`) / COUNT(`BetID`)),2),
ROUND((((SUM(`BetAmount`) + SUM(`Payout`)) / SUM(`Payout`)) * 100),2)
FROM `betdb`
A simple SQLfiddle to test with.
Use FORMAT((SUM(BetAmount) - SUM(Payout)),0)
Try this:
SELECT COUNT(`BetID`),
FORMAT(SUM(`BetAmount`),0),
FORMAT(SUM(`Payout`),0),
FORMAT((SUM(`BetAmount`) - SUM(`Payout`)),0),
ROUND((SUM(`BetAmount`) / COUNT(`BetID`)),2),
ROUND((((SUM(`BetAmount`) + SUM(`Payout`)) / SUM(`Payout`)) * 100),2)
FROM `betdb`
You could also try using a join statement so that the calculation is only done once:
SELECT *,t.BetTotal - t.PayoutTotal as Difference
FROM (
SELECT
COUNT(`BetID`) AS Count,
FORMAT(SUM(`BetAmount`),0) as BetTotal,
FORMAT(SUM(`Payout`),0) as PayoutTotal,
ROUND((SUM(`BetAmount`) / COUNT(`BetID`)),2),
ROUND((((SUM(`BetAmount`) + SUM(`Payout`)) / SUM(`Payout`)) * 100),2)
FROM `betdb`
) as t

MySQL how to select the nearest values to my variables

I have the following query where Im trying to retrieve matches, within a certain breathing space, of the variables entered.
SELECT fthg, ftag, avover, avunder, whh, wha, whd
FROM full
WHERE (whh < ($home_odds + 0.05)
AND whh > ($home_odds - 0.05)
AND wha < ($away_odds + 0.05)
AND wha > ($away_odds -0.05)
AND whd < ($draw_odds + 0.05)
AND whd > ($draw_odds - 0.05))
There are occasions where this returns 0 results so in that case I would like to retrieve the closest matching record to all three but Im not quite sure how to put the query together.
Basically this is the last resort if the other query doesn't return results, this one will return the next best thing no matter how far from the original values.
Thanks for the help
Your original query would be simpler and more readable as this:
SELECT
fthg,
ftag,
avover,
avunder,
whh,
wha,
whd
FROM full
WHERE ABS($home_odds - whh) < 0.05
and ABS($away_odds - wha) < 0.05
and ABS($draw_odds - whd) < 0.05
If that query returns nothing, you could run this one:
SELECT
fthg,
ftag,
avover,
avunder,
whh,
wha,
whd
FROM full
ORDER BY
ABS($home_odds - whh) + ABS($away_odds - wha) + ABS($draw_odds - whd)
LIMIT 1
It will return the row with the lowest deviation from the combination of those three pairs of fields.
How about faking a distance calculation between the parameters you provide and the parameters you are comparing to? Something like
SELECT fthg, ftag, avover, avunder, whh, wha, whd
FROM full
ORDER BY
sqrt(abs(whh - $home_odds) * abs(whh - $home_odds)) +
sqrt(abs(wha - $away_odds) * abs(wha - $away_odds)) +
sqrt(abs(whd - $draw_odds) * abs(whd - $draw_odds))
This way, even if there are no matches given the range you are interested in, you can still get a closer result.

MySQL select the nearest lower value in table

I have an SQL table that stores running times and a score associated with each time on the table.
/////////////////////
/ Time * Score /
/ 1531 * 64 /
/ 1537 * 63 /
/ 1543 * 61 /
/ 1549 * 60 /
/////////////////////
This is an example of 4 rows in the table. My question is how do I select the nearest lowest time.
EXAMPLE: If someone records a time of 1548 I want to return the score for 1543 (not 1549) which is 61.
Is there an SQL query I can use to do this thank you.
Use SQL's WHERE clause to filter the records, its ORDER BY clause to sort them and LIMIT (in MySQL) to obtain only the first result:
SELECT Score
FROM my_table
WHERE Time <= 1548
ORDER BY Time DESC
LIMIT 1
See it on sqlfiddle.