Question

I want to get all records have price 20% Less OR More of a give value.

+----+------------+----------+---------+
| ID | Item        | Category |  price  |       
+----+------------+----------+-----------+
|  1 | Popular     | Rock     |  1000
|  2 | Classical   | Opera    |  5000 
|  3 | Popular2    | Jazz     |  6000
|  4 | Classical2  | Dance    |  8000
|  5 | Classical3  | General  |  4825
+----+------------+----------+------------+

User has to pass two parameters

i - Category

ii-Price

I need to show all item having same category and price is between 20% less and 20% greater than user's value. Something like this

SELECT * FROM Table1 WHERE Categry='ROCK' AND price > (20% less price of user's price) AND price < (20% greater than user's price)
Était-ce utile?

La solution

Assuming @price represents the user's price:

SELECT * 
FROM   Table1 
WHERE  Categry='ROCK' AND price BETWEEN (0.8 * @price) AND price < (1.2 * @price)

Autres conseils

SELECT
    *
FROM
    Table1
WHERE
    Categry='ROCK'
    AND price > (userprice * 0.8)
    AND price < (userprice * 1.2)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top