How to write Mysql query to compare variable value with column values and get top highest record or second...
I have table in mysql with few records, so I want to compare value with price column.
- if passing value from variable is less than price column value then get second highest record with product id ="p1"
- 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
add a comment |
I have table in mysql with few records, so I want to compare value with price column.
- if passing value from variable is less than price column value then get second highest record with product id ="p1"
- 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
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
add a comment |
I have table in mysql with few records, so I want to compare value with price column.
- if passing value from variable is less than price column value then get second highest record with product id ="p1"
- 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
I have table in mysql with few records, so I want to compare value with price column.
- if passing value from variable is less than price column value then get second highest record with product id ="p1"
- 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
mysql
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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 |
+----+---------+-------+------+
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 |
+----+---------+-------+------+
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
add a comment |
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 |
+----+---------+-------+------+
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
add a comment |
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 |
+----+---------+-------+------+
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 |
+----+---------+-------+------+
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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