Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL查询中添加条件语句?_Sql_Postgresql - Fatal编程技术网

如何在SQL查询中添加条件语句?

如何在SQL查询中添加条件语句?,sql,postgresql,Sql,Postgresql,嗨,我正在处理一个SQL查询。下面是我执行查询的方法名 private string GetAggregatedOptionParametersByStyleIdCommand(string styleId, bool currentStore) { SELECT opts.style, opts.option::text as {OptionKey}, opts.primary_colour, opts.secondary_colour, opts.brand_

嗨,我正在处理一个SQL查询。下面是我执行查询的方法名

private string GetAggregatedOptionParametersByStyleIdCommand(string styleId, bool currentStore)
    {
      SELECT
      opts.style, opts.option::text as {OptionKey}, opts.primary_colour, opts.secondary_colour, opts.brand_description, opts.description, params.*,
    CASE WHEN {currentStore} == true THEN 
    FROM
      rex.options opts
    JOIN
      rex.product_atoms atoms ON atoms.option_id = opts.option
    JOIN
      rex.parameters params ON atoms.id = params.product_atom_id
    JOIN
      rex.stores stores ON params.store = stores.id
    WHERE
      opts.style = '{styleId}'
  }
下面是stores表的结构

CREATE TABLE rex.stores (
    id serial NOT NULL,
    close_date timestamp NOT NULL,
    country text NULL,
    distribution_centre text NULL,
    "name" text NULL,
    open_date timestamp NOT NULL,
    CONSTRAINT "PK_stores" PRIMARY KEY (id)
);
所以我想说的是,只要currentStore为true,那么我就要返回当前存储。检查currentStore的条件为

 store.OpenDate <= currentDate &&
          store.CloseDate >= currentDate

我试图在SQL查询中添加这些条件。当{currentStore}==true时,我添加了
CASE,然后
我不确定如何添加关闭的存储条件。有人能帮我完成这个查询吗?任何帮助都将不胜感激。多谢各位

如果您想检查另一种可能性,您可以在
案例
语句中添加另一个
WHEN
ELSE
条件。这些需要在
结束之前出现

例如:

CASE
    WHEN x < 0 THEN 'negative'
    WHEN x > 0 THEN 'positive'
    ELSE 'zero'
END
案例
当x<0时,则为“负”
当x>0时,则为“正”
否则为“零”
结束

我不熟悉postgres,你可能不需要一个
结尾
,你也可以说
CASE x WHEN>0然后。。。当<0…
表示在
案例之后只需提及x一次
;同样,这在postgres中可能不是一件事。

如果你想检查另一种可能性,你可以在
时添加另一个
,或者在
案例中添加另一个
条件。这些需要在
结束之前出现

例如:

CASE
    WHEN x < 0 THEN 'negative'
    WHEN x > 0 THEN 'positive'
    ELSE 'zero'
END
案例
当x<0时,则为“负”
当x>0时,则为“正”
否则为“零”
结束

我不熟悉postgres,你可能不需要一个
结尾
,你也可以说
CASE x WHEN>0然后。。。当<0…
表示在
案例之后只需提及x一次
;同样,这在postgres中可能不是一件事。

这是什么DBMS?根据
演员阵容判断,可能是Postgres?你的功能所在的语言是什么?也许吧?请添加适当的标签,好吗?这是什么DBMS?根据
演员阵容判断,可能是Postgres?你的功能所在的语言是什么?也许吧?请添加相应的标签,好吗?