在 SQL 查询的 WHERE 子句中模拟 if 条件
Simulating an if condition in the WHERE clause of an SQL query
我正在尝试根据以下条件将 google BigQuery 写入 select 一些记录:
if ID_parameter is not null:
SELECT * WHERE ID=ID_parameter
else:
SELECT * WHERE country_id=country_id_parameter and (name LIKE %name_parameter% OR name is null)
name_parameter 应该是可选的,但必须提供 ID_parameter 或 country_id_parameter 之一。
对不起,我是SQL的新手,但我希望我说清楚我想要什么。
我在查询中遗漏了 table 名称,我们假设它是 'personnel'。
假设 ID-parameter = 200, country_id_parameter = 'NL', name_parameter = '%bart%'
名字是'bart'时可能是这样:
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL' and
name LIKE '%bart%')
当名字为 NULL 时使用这个:
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL')
考虑以下方法
select *
from your_table
where case
when not id_parameter is null then id = id_parameter
else country_id = country_id_parameter and (regexp_contains(name, name_parameter) or name is null)
end
我正在尝试根据以下条件将 google BigQuery 写入 select 一些记录:
if ID_parameter is not null:
SELECT * WHERE ID=ID_parameter
else:
SELECT * WHERE country_id=country_id_parameter and (name LIKE %name_parameter% OR name is null)
name_parameter 应该是可选的,但必须提供 ID_parameter 或 country_id_parameter 之一。
对不起,我是SQL的新手,但我希望我说清楚我想要什么。
我在查询中遗漏了 table 名称,我们假设它是 'personnel'。 假设 ID-parameter = 200, country_id_parameter = 'NL', name_parameter = '%bart%'
名字是'bart'时可能是这样:
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL' and
name LIKE '%bart%')
当名字为 NULL 时使用这个:
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL')
考虑以下方法
select *
from your_table
where case
when not id_parameter is null then id = id_parameter
else country_id = country_id_parameter and (regexp_contains(name, name_parameter) or name is null)
end