Page 1 of 1

SQL query other than "case when...then"

Posted: 2021-09-09 13:38
by sacgtdev
Table A
Value
3

How to do a sql query to assign 'medium' based on the value in the Table A? The category will be referenced from Table B.
I can think of case when...then (hard-code) in the sql query. Is there any alternative way to write the query?

Table B
Category Min Max
Low 0 2.99
Medium 3 4.99
High 5 5.99

Re: SQL query other than "case when...then"

Posted: 2021-09-09 17:04
by onoehring
Hi,

try something like this (pseudo code)

Code: Select all

$sql = "select category from tableB where min <= " . $value . " AND max >" . $value . ";"
$result= sqlValue($sql)
PS: You need only min or max in your table, as the other one automatically is pure logic - your SQL will probably get more complex
PSPS: You should use different fieldnames (not min and max).

Olaf

Re: SQL query other than "case when...then"

Posted: 2021-09-10 09:37
by sacgtdev
Thanks. Your suggestion work in php.

Just wonder how to implement this to the calculated field. As I now there is pesudo code for id, primary key and table..

but, how to custom a pseudo-code?