根据参数显示列
Show column according to parameter
我正在尝试编写 SQL 脚本来显示基于用户参数的列。我无法在任何地方找到可接受的答案。
这是我想做的事情的简化版本(在 OracleDB 中):
SELECT Table_car.purchase_date AS PurchaseDate,
Table_car.sale_date AS SaleDate,
CASE WHEN :userinput <> 999 THEN (SELECT Table_car.car_brand FROM Table_car WHERE Table_car.car_brand = :userinput )
ELSE (SELECT Table_car.car_brand FROM Table_car)
END AS CarBrand
FROM Table_car
我想要的伪代码可以是
if(user_input = 999)
show_all_values_of(Table_car.car_brand );
else
show_only_some_values_of(Table_car.car_brand, user_input);
这是预期的结果:
if user_input <> 999 (if is 1, for example)
| PurchaseDate | SaleDate | CarBrand |
+--------------+------------+----------+
| 03/12/2018 | 09/10/2021 | 1 |
----------------------------------------
| 13/06/2011 | 20/11/2021 | 1 |
----------------------------------------
| 22/01/2020 | 09/12/2021 | 1 |
----------------------------------------
else (if user input is 999)
| PurchaseDate | SaleDate | CarBrand |
+--------------+------------+----------+
| 03/12/2018 | 09/10/2021 | 1 |
----------------------------------------
| 13/06/2001 | 20/11/2021 | 5 |
----------------------------------------
| 22/01/2020 | 09/12/2021 | 1 |
----------------------------------------
| 03/12/2018 | 09/10/2021 | 3 |
----------------------------------------
| 13/06/2012 | 10/10/2020 | 9 |
----------------------------------------
| 22/01/2020 | 09/12/2020 | 2 |
----------------------------------------
您可以将逻辑放入 where
子句中,使用 or
;类似于:
SELECT Table_car.purchase_date AS PurchaseDate,
Table_car.sale_date AS SaleDate,
Table_car.car_brand AS CarBrand
FROM Table_car
WHERE :userinput = 999 OR Table_car.car_brand = :userinput
如果您的列可以为空,您可能需要额外的逻辑以特定方式处理它,但看起来不应该如此。如果您需要 where
中的其他条件,请记住在上面的逻辑周围加上括号,以确保它按照您的意图进行解释;即
WHERE (:userinput = 999 OR Table_car.car_brand = :userinput)
AND ... something else
顺便说一句,您可能需要考虑使用 table 别名来减少输入。
我正在尝试编写 SQL 脚本来显示基于用户参数的列。我无法在任何地方找到可接受的答案。
这是我想做的事情的简化版本(在 OracleDB 中):
SELECT Table_car.purchase_date AS PurchaseDate,
Table_car.sale_date AS SaleDate,
CASE WHEN :userinput <> 999 THEN (SELECT Table_car.car_brand FROM Table_car WHERE Table_car.car_brand = :userinput )
ELSE (SELECT Table_car.car_brand FROM Table_car)
END AS CarBrand
FROM Table_car
我想要的伪代码可以是
if(user_input = 999)
show_all_values_of(Table_car.car_brand );
else
show_only_some_values_of(Table_car.car_brand, user_input);
这是预期的结果:
if user_input <> 999 (if is 1, for example)
| PurchaseDate | SaleDate | CarBrand |
+--------------+------------+----------+
| 03/12/2018 | 09/10/2021 | 1 |
----------------------------------------
| 13/06/2011 | 20/11/2021 | 1 |
----------------------------------------
| 22/01/2020 | 09/12/2021 | 1 |
----------------------------------------
else (if user input is 999)
| PurchaseDate | SaleDate | CarBrand |
+--------------+------------+----------+
| 03/12/2018 | 09/10/2021 | 1 |
----------------------------------------
| 13/06/2001 | 20/11/2021 | 5 |
----------------------------------------
| 22/01/2020 | 09/12/2021 | 1 |
----------------------------------------
| 03/12/2018 | 09/10/2021 | 3 |
----------------------------------------
| 13/06/2012 | 10/10/2020 | 9 |
----------------------------------------
| 22/01/2020 | 09/12/2020 | 2 |
----------------------------------------
您可以将逻辑放入 where
子句中,使用 or
;类似于:
SELECT Table_car.purchase_date AS PurchaseDate,
Table_car.sale_date AS SaleDate,
Table_car.car_brand AS CarBrand
FROM Table_car
WHERE :userinput = 999 OR Table_car.car_brand = :userinput
如果您的列可以为空,您可能需要额外的逻辑以特定方式处理它,但看起来不应该如此。如果您需要 where
中的其他条件,请记住在上面的逻辑周围加上括号,以确保它按照您的意图进行解释;即
WHERE (:userinput = 999 OR Table_car.car_brand = :userinput)
AND ... something else
顺便说一句,您可能需要考虑使用 table 别名来减少输入。