如何根据条件在数据表中选择列名。例如,根据字段名称必须包含子字符串作为"price"
的条件。
DataTable如下
id | name place | car_price | bike_price
试试这个:
select column_name
from information_schema.columns
where table_schema = 'db_name'
and table_name = 'table_name'
and column_name like '%price%';
这将列出所有field_name,其中“price”为表中的子字符串。
如果要有条件地为talble上的查询的结果集中的任何给定记录选择car_price或bike_price,可以使用CASE
语句,如下所示:
SELECT name,
CASE
WHEN name LIKE '%car%' THEN car_price -- your condition here
ELSE bike_price
END AS price
FROM dataTable