I a have a table name(weight) in database
id | 1000 | 500 | 300 | 100 | 1 |
---|---|---|---|---|---|
1 | 60 | 120 | 140 | 180 | 200 |
for 1000 (1kg)time required is 60
for 500 (.5kg)time required is 120 and so on
based on this weight data should be fetched from the table
If the entered weight by the user is 700 it should fetch the data of 500 which is the less than 700 and return the value 120
so my code must be like
SELECT (column name)<700 FROM Weight WHERE id=1
Thanks:)
It will be much easy if you rearrange your data like:
id, condition, value
1, 1000 60
1, 500 120
1, 300 140
and the query would be like this:
SELECT value FROM table WHERE id=1 AND condition < 700 ORDER BY condition DESC LIMIT 1
Your approach is not efficient to get your desired results.
Update your table:weight with the below one.
SQL:
CREATE TABLE `weight` (
`id` int(11) NOT NULL,
`weight` int(11) NOT NULL,
`time` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `weight` (`id`, `weight`, `time`) VALUES
(1, 100, 60),
(2, 500, 120),
(3, 300, 140),
(4, 100, 180),
(5, 1, 200);
ALTER TABLE `weight`
ADD PRIMARY KEY (`id`);
ALTER TABLE `weight`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
COMMIT;
Add use the following query to get your desired results.
SELECT time FROM weight WHERE weight<=700 ORDER BY weight DESC LIMIT 1
You can use case.. when
in select
clause as follows:
Select case when your_value >= 1000 then column_1000
when your_value >= 500 then column_500
when your_value >= 300 then column_300
when your_value >= 100 then column_100
Else column_1
End as res_
From your_table t
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.