How to write Mysql query to compare variable value with column values and get top highest record or second...












-1















I have table in mysql with few records, so I want to compare value with price column.




  1. if passing value from variable is less than price column value then get second highest record with product id ="p1"

  2. if passing value from variable is greater then price column values then get top highest record with product id= "p1"


This my table



id  product price
1 p1 2000
2 p2 3000
3 p3 4000
4 p1 2500


if we want to compare ' price ' column with value $var='1800' and product="p1" then output should be second highest record because variable value is less than price column value



output :



id  product  price
1 p1 2000


but if we pass value $var=5000 for price column and product="p1" then



output should be :



id  product  price
4 p1 2500









share|improve this question

























  • What will be the output when input variable is 2200 and product is p1. Now one product has price (=2500) higher than 2200, and another has price (=2000) lower than 2200.

    – Madhur Bhaiya
    Nov 25 '18 at 11:55











  • @MadhurBhaiya if input variable is 2200 and product is p1 then get for 2500 price

    – Anonymous
    Nov 25 '18 at 11:59
















-1















I have table in mysql with few records, so I want to compare value with price column.




  1. if passing value from variable is less than price column value then get second highest record with product id ="p1"

  2. if passing value from variable is greater then price column values then get top highest record with product id= "p1"


This my table



id  product price
1 p1 2000
2 p2 3000
3 p3 4000
4 p1 2500


if we want to compare ' price ' column with value $var='1800' and product="p1" then output should be second highest record because variable value is less than price column value



output :



id  product  price
1 p1 2000


but if we pass value $var=5000 for price column and product="p1" then



output should be :



id  product  price
4 p1 2500









share|improve this question

























  • What will be the output when input variable is 2200 and product is p1. Now one product has price (=2500) higher than 2200, and another has price (=2000) lower than 2200.

    – Madhur Bhaiya
    Nov 25 '18 at 11:55











  • @MadhurBhaiya if input variable is 2200 and product is p1 then get for 2500 price

    – Anonymous
    Nov 25 '18 at 11:59














-1












-1








-1








I have table in mysql with few records, so I want to compare value with price column.




  1. if passing value from variable is less than price column value then get second highest record with product id ="p1"

  2. if passing value from variable is greater then price column values then get top highest record with product id= "p1"


This my table



id  product price
1 p1 2000
2 p2 3000
3 p3 4000
4 p1 2500


if we want to compare ' price ' column with value $var='1800' and product="p1" then output should be second highest record because variable value is less than price column value



output :



id  product  price
1 p1 2000


but if we pass value $var=5000 for price column and product="p1" then



output should be :



id  product  price
4 p1 2500









share|improve this question
















I have table in mysql with few records, so I want to compare value with price column.




  1. if passing value from variable is less than price column value then get second highest record with product id ="p1"

  2. if passing value from variable is greater then price column values then get top highest record with product id= "p1"


This my table



id  product price
1 p1 2000
2 p2 3000
3 p3 4000
4 p1 2500


if we want to compare ' price ' column with value $var='1800' and product="p1" then output should be second highest record because variable value is less than price column value



output :



id  product  price
1 p1 2000


but if we pass value $var=5000 for price column and product="p1" then



output should be :



id  product  price
4 p1 2500






mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 12:24







Anonymous

















asked Nov 25 '18 at 11:53









AnonymousAnonymous

231112




231112













  • What will be the output when input variable is 2200 and product is p1. Now one product has price (=2500) higher than 2200, and another has price (=2000) lower than 2200.

    – Madhur Bhaiya
    Nov 25 '18 at 11:55











  • @MadhurBhaiya if input variable is 2200 and product is p1 then get for 2500 price

    – Anonymous
    Nov 25 '18 at 11:59



















  • What will be the output when input variable is 2200 and product is p1. Now one product has price (=2500) higher than 2200, and another has price (=2000) lower than 2200.

    – Madhur Bhaiya
    Nov 25 '18 at 11:55











  • @MadhurBhaiya if input variable is 2200 and product is p1 then get for 2500 price

    – Anonymous
    Nov 25 '18 at 11:59

















What will be the output when input variable is 2200 and product is p1. Now one product has price (=2500) higher than 2200, and another has price (=2000) lower than 2200.

– Madhur Bhaiya
Nov 25 '18 at 11:55





What will be the output when input variable is 2200 and product is p1. Now one product has price (=2500) higher than 2200, and another has price (=2000) lower than 2200.

– Madhur Bhaiya
Nov 25 '18 at 11:55













@MadhurBhaiya if input variable is 2200 and product is p1 then get for 2500 price

– Anonymous
Nov 25 '18 at 11:59





@MadhurBhaiya if input variable is 2200 and product is p1 then get for 2500 price

– Anonymous
Nov 25 '18 at 11:59












1 Answer
1






active

oldest

votes


















0














DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,product INT NOT NULL
,price INT NOT NULL
);

INSERT INTO my_table VALUES
(1,1,2000),
(2,2,3000),
(3,3,4000),
(4,1,2500);

mysql> SELECT *, ABS(price-1800) x FROM my_table WHERE product = 1 ORDER BY x LIMIT 1;
+----+---------+-------+-----+
| id | product | price | x |
+----+---------+-------+-----+
| 1 | 1 | 2000 | 200 |
+----+---------+-------+-----+
1 row in set (0.00 sec)

mysql> SELECT *, ABS(price-5000) x FROM my_table WHERE product = 1 ORDER BY x LIMIT 1;
+----+---------+-------+------+
| id | product | price | x |
+----+---------+-------+------+
| 4 | 1 | 2500 | 2500 |
+----+---------+-------+------+





share|improve this answer
























  • but when I pass value 2200 it is getting record 2000 but I want record should be 2500

    – Anonymous
    Nov 25 '18 at 12:30











  • can you please suggest me query which should work like if value is 1800 then get price column 2000 but if value is 2200 or 5500 then get price column for 2500

    – Anonymous
    Nov 25 '18 at 12:33











  • Not without knowing the general logic behind such a choice

    – Strawberry
    Nov 25 '18 at 12:35











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467145%2fhow-to-write-mysql-query-to-compare-variable-value-with-column-values-and-get-to%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,product INT NOT NULL
,price INT NOT NULL
);

INSERT INTO my_table VALUES
(1,1,2000),
(2,2,3000),
(3,3,4000),
(4,1,2500);

mysql> SELECT *, ABS(price-1800) x FROM my_table WHERE product = 1 ORDER BY x LIMIT 1;
+----+---------+-------+-----+
| id | product | price | x |
+----+---------+-------+-----+
| 1 | 1 | 2000 | 200 |
+----+---------+-------+-----+
1 row in set (0.00 sec)

mysql> SELECT *, ABS(price-5000) x FROM my_table WHERE product = 1 ORDER BY x LIMIT 1;
+----+---------+-------+------+
| id | product | price | x |
+----+---------+-------+------+
| 4 | 1 | 2500 | 2500 |
+----+---------+-------+------+





share|improve this answer
























  • but when I pass value 2200 it is getting record 2000 but I want record should be 2500

    – Anonymous
    Nov 25 '18 at 12:30











  • can you please suggest me query which should work like if value is 1800 then get price column 2000 but if value is 2200 or 5500 then get price column for 2500

    – Anonymous
    Nov 25 '18 at 12:33











  • Not without knowing the general logic behind such a choice

    – Strawberry
    Nov 25 '18 at 12:35
















0














DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,product INT NOT NULL
,price INT NOT NULL
);

INSERT INTO my_table VALUES
(1,1,2000),
(2,2,3000),
(3,3,4000),
(4,1,2500);

mysql> SELECT *, ABS(price-1800) x FROM my_table WHERE product = 1 ORDER BY x LIMIT 1;
+----+---------+-------+-----+
| id | product | price | x |
+----+---------+-------+-----+
| 1 | 1 | 2000 | 200 |
+----+---------+-------+-----+
1 row in set (0.00 sec)

mysql> SELECT *, ABS(price-5000) x FROM my_table WHERE product = 1 ORDER BY x LIMIT 1;
+----+---------+-------+------+
| id | product | price | x |
+----+---------+-------+------+
| 4 | 1 | 2500 | 2500 |
+----+---------+-------+------+





share|improve this answer
























  • but when I pass value 2200 it is getting record 2000 but I want record should be 2500

    – Anonymous
    Nov 25 '18 at 12:30











  • can you please suggest me query which should work like if value is 1800 then get price column 2000 but if value is 2200 or 5500 then get price column for 2500

    – Anonymous
    Nov 25 '18 at 12:33











  • Not without knowing the general logic behind such a choice

    – Strawberry
    Nov 25 '18 at 12:35














0












0








0







DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,product INT NOT NULL
,price INT NOT NULL
);

INSERT INTO my_table VALUES
(1,1,2000),
(2,2,3000),
(3,3,4000),
(4,1,2500);

mysql> SELECT *, ABS(price-1800) x FROM my_table WHERE product = 1 ORDER BY x LIMIT 1;
+----+---------+-------+-----+
| id | product | price | x |
+----+---------+-------+-----+
| 1 | 1 | 2000 | 200 |
+----+---------+-------+-----+
1 row in set (0.00 sec)

mysql> SELECT *, ABS(price-5000) x FROM my_table WHERE product = 1 ORDER BY x LIMIT 1;
+----+---------+-------+------+
| id | product | price | x |
+----+---------+-------+------+
| 4 | 1 | 2500 | 2500 |
+----+---------+-------+------+





share|improve this answer













DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,product INT NOT NULL
,price INT NOT NULL
);

INSERT INTO my_table VALUES
(1,1,2000),
(2,2,3000),
(3,3,4000),
(4,1,2500);

mysql> SELECT *, ABS(price-1800) x FROM my_table WHERE product = 1 ORDER BY x LIMIT 1;
+----+---------+-------+-----+
| id | product | price | x |
+----+---------+-------+-----+
| 1 | 1 | 2000 | 200 |
+----+---------+-------+-----+
1 row in set (0.00 sec)

mysql> SELECT *, ABS(price-5000) x FROM my_table WHERE product = 1 ORDER BY x LIMIT 1;
+----+---------+-------+------+
| id | product | price | x |
+----+---------+-------+------+
| 4 | 1 | 2500 | 2500 |
+----+---------+-------+------+






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 12:26









StrawberryStrawberry

25.9k83149




25.9k83149













  • but when I pass value 2200 it is getting record 2000 but I want record should be 2500

    – Anonymous
    Nov 25 '18 at 12:30











  • can you please suggest me query which should work like if value is 1800 then get price column 2000 but if value is 2200 or 5500 then get price column for 2500

    – Anonymous
    Nov 25 '18 at 12:33











  • Not without knowing the general logic behind such a choice

    – Strawberry
    Nov 25 '18 at 12:35



















  • but when I pass value 2200 it is getting record 2000 but I want record should be 2500

    – Anonymous
    Nov 25 '18 at 12:30











  • can you please suggest me query which should work like if value is 1800 then get price column 2000 but if value is 2200 or 5500 then get price column for 2500

    – Anonymous
    Nov 25 '18 at 12:33











  • Not without knowing the general logic behind such a choice

    – Strawberry
    Nov 25 '18 at 12:35

















but when I pass value 2200 it is getting record 2000 but I want record should be 2500

– Anonymous
Nov 25 '18 at 12:30





but when I pass value 2200 it is getting record 2000 but I want record should be 2500

– Anonymous
Nov 25 '18 at 12:30













can you please suggest me query which should work like if value is 1800 then get price column 2000 but if value is 2200 or 5500 then get price column for 2500

– Anonymous
Nov 25 '18 at 12:33





can you please suggest me query which should work like if value is 1800 then get price column 2000 but if value is 2200 or 5500 then get price column for 2500

– Anonymous
Nov 25 '18 at 12:33













Not without knowing the general logic behind such a choice

– Strawberry
Nov 25 '18 at 12:35





Not without knowing the general logic behind such a choice

– Strawberry
Nov 25 '18 at 12:35


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467145%2fhow-to-write-mysql-query-to-compare-variable-value-with-column-values-and-get-to%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)