You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SQL查询中添加动态门店过滤条件?SQL语句编写求助

完善基于currentStore参数过滤门店的SQL查询

我来帮你搞定这个查询问题!你的核心需求是根据currentStore参数动态筛选门店数据,但之前的CASE WHEN用错了位置——过滤逻辑应该放在WHERE子句里,而不是SELECT列表中。下面是修正后的完整实现:

修正后的C#方法

private string GetAggregatedOptionParametersByStyleIdCommand(string styleId, bool currentStore) {
    // 根据currentStore参数生成对应的门店过滤条件
    var storeFilterCondition = currentStore 
        ? "stores.open_date <= CURRENT_DATE AND stores.close_date >= CURRENT_DATE"
        : "stores.close_date >= CURRENT_DATE";

    // 拼接完整的SQL语句
    return $@"
        SELECT opts.style, 
               opts.option::text AS ""OptionKey"", 
               opts.primary_colour, 
               opts.secondary_colour, 
               opts.brand_description, 
               opts.description, 
               params.*
        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}'
          AND {storeFilterCondition}";
}

关键修改点说明

  1. 动态生成门店过滤条件:
    • 当currentStore = true时,筛选当前正在营业的门店:要求门店的开业日期≤当前日期,且关闭日期≥当前日期
    • 当currentStore = false时,筛选所有未关闭的门店:只要求关闭日期≥当前日期(不管是否已经开业)
  2. 修正SQL语法问题:
    • 移除了你原来未完成的CASE WHEN语句(那部分逻辑不属于SELECT返回字段)
    • 用双引号包裹"OptionKey"(PostgreSQL中如果标识符包含大写字母,需要用双引号确保正确识别)
  3. 代码可读性优化:
    把门店过滤逻辑单独提取出来,用C#的三元运算符生成,比直接在SQL里嵌套复杂条件更清晰

重要安全提醒:避免SQL注入

直接把styleId拼接进SQL字符串存在严重的SQL注入风险!强烈建议改用参数化查询,比如使用Dapper或原生SqlCommand(以PostgreSQL的Npgsql为例):

// 更安全的参数化查询示例(使用Dapper)
using Dapper;
using Npgsql;

private async Task<IEnumerable<YourResultModel>> GetAggregatedOptionParametersByStyleIdAsync(string styleId, bool currentStore) {
    var storeFilterCondition = currentStore 
        ? "stores.open_date <= CURRENT_DATE AND stores.close_date >= CURRENT_DATE"
        : "stores.close_date >= CURRENT_DATE";

    var sql = $@"
        SELECT opts.style, 
               opts.option::text AS ""OptionKey"", 
               opts.primary_colour, 
               opts.secondary_colour, 
               opts.brand_description, 
               opts.description, 
               params.*
        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
          AND {storeFilterCondition}";

    using(var connection = new NpgsqlConnection(YourDatabaseConnectionString)) {
        await connection.OpenAsync();
        return await connection.QueryAsync<YourResultModel>(sql, new { StyleId = styleId });
    }
}

额外说明

  • 如果你的数据库不是PostgreSQL,需要调整日期函数:比如SQL Server用GETDATE(),MySQL用CURDATE()
  • YourResultModel是你自定义的实体类,用于映射查询返回的字段

内容的提问来源于stack exchange,提问作者Niranjan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:12:36