I have a query that checks the value of some case from DB and finds the rank in witch that value resides in table BOD
:
SELECT DISTINCT bod_bod, (SELECT REPLACE(predf_VPS,'.','') as vps FROM PREDMETIFView WHERE
predf_nas_br=?) as vps_baza FROM BOD WHERE (SELECT REPLACE(predf_VPS,'.','') as vps
FROM PREDMETIFView WHERE predf_nas_br=?) BETWEEN bod_od AND bod_do ORDER BY bod_bod ASC
LIMIT 1
Table BOD is:
| bod_id | bod_od | bod_do | bod_bod | bod_dodan |
| ------ | --------- | --------- | ------- | ------------------- |
| 0 | 0.00 | 2500.00 | 25 | 0000-00-00 00:00:00 |
| 1 | 2500.01 | 5000.00 | 50 | 0000-00-00 00:00:00 |
| 2 | 5000.01 | 10000.00 | 75 | 0000-00-00 00:00:00 |
| 3 | 10000.01 | 100000.00 | 100 | 2020-04-10 13:06:53 |
| 4 | 100000.01 | 250000.00 | 250 | 0000-00-00 00:00:00 |
| 5 | 250000.01 | 500000.00 | 500 | 2020-04-08 23:04:30 |
Current SQL for the sub-query value 11899
from PREDMETIFView
table select gives me result:
bod_bod vps_baza
100 11899
because number 11899
is between 10000.01
and 100000.00
.
I later do some calculations with that number in PHP.
But here is the problem: When the number is bigger then 500000.00, result set from query is 0 rows (because there is no rank that matches). But I still need that number vps_baza
to do calculations in PHP.
So is there a way to say if there is no ranked matches that it still produces one result row (without altering table BOD) of for example for vps_baza 511899
value:
bod_bod vps_baza
null 511899
MySQL version is 5.6
I have made an DBfiddle:
If you change value on line 98
from 511899
to for example 899
, i get the row, but bigger values then 500000
it fails.
You can use left join
:
select x.vps_baza, . . .
from (select 11899 as vps_baza) x left join
bods b
on x.vps.baza between b.bod_od and b.bod_do;
Note: This answer is based on your question and sample data. It is quite unclear to me what the query has to do with either of those.
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.