【问题标题】:Mysql multiple addwithvalue: one returned value is enoughmysql多个addwithvalue:一个返回值就够了
【发布时间】:2013-03-10 15:45:19
【问题描述】:

我有一个包含多个 addwithvalue 值的查询

        IDCheck.CommandText = "SELECT * FROM cloudposgebruikers WHERE id = @id AND licentie1hc = @lc1 AND licentie2hc = @lc2"
        + " AND licentie3hc = @lc3 AND licentie4hc = @lc4 AND licentie5hc = @lc5 AND licentie6hc = @lc6 AND licentie7hc = @LC7 AND licentie8hc = @lc8"
        + " AND licentie9hc = @lc9 AND licentie10hc = @lc10";
        IDCheck.Parameters.AddWithValue("@id", licenseid);
        IDCheck.Parameters.AddWithValue("lc1", GetId);
        IDCheck.Parameters.AddWithValue("lc2", GetId);
        IDCheck.Parameters.AddWithValue("lc3", GetId);
        IDCheck.Parameters.AddWithValue("lc4", GetId);
        IDCheck.Parameters.AddWithValue("lc5", GetId);
        IDCheck.Parameters.AddWithValue("lc6", GetId);
        IDCheck.Parameters.AddWithValue("lc7", GetId);
        IDCheck.Parameters.AddWithValue("lc8", GetId);
        IDCheck.Parameters.AddWithValue("lc9", GetId);
        IDCheck.Parameters.AddWithValue("lc10", GetId);

当然,这将比较所有这些值,并且只有在所有值都存在时才返回 true

假设只有 "licentie5hc" 和 "id" 匹配 => 返回 true

或者让我们说只有“id”和licentie1hc“匹配.. =>返回true

这可能吗?我知道我可以使用不同的查询,但我只需要“id”和“licentie x hc”参数之一来匹配...

【问题讨论】:

    标签: c# mysql parameters


    【解决方案1】:

    试试这个:

    IDCheck.CommandText = "SELECT * FROM cloudposgebruikers 
        WHERE id = @id AND (licentie1hc = @lc1 OR licentie2hc = @lc2 
            OR licentie3hc = @lc3 OR licentie4hc = @lc4 OR licentie5hc = @lc5 
            OR licentie6hc = @lc6 OR licentie7hc = @LC7 OR 
            licentie8hc = @lc8 OR licentie9hc = @lc9 OR licentie10hc = @lc10)";
    

    相当于:

    SELECT * FROM TABLE
    WHERE id=@id AND(lic1=@lic1 OR lic2=@lic2...etc...OR lic10=@lc10);
    

    注意:我知道您可能无法更改数据库结构,但是您这样做有十列,表明您的数据库可以从一些重构中受益。

    【讨论】:

    • 如果我有足够的代表,我会给你一个 +1 的快速和好的答案。谢谢!!
    • 谢谢!查看 rs 的回答,他发现了一些我没有大大简化您的代码的东西。
    • 呵呵,不,我们不能改变我们的数据库结构,它是我们许可系统的一部分,所以没有其他选择。但无论如何谢谢:D
    【解决方案2】:

    将您的查询更改为使用 OR 而不是 AND,您不必为相同的值使用不同的参数,试试这个,

    IDCheck.CommORText = "SELECT * FROM cloudposgebruikers WHERE id = @id OR licentie1hc = @lc OR licentie2hc = @lc"
            + " OR licentie3hc = @lc OR licentie4hc = @lc OR licentie5hc = @lc OR licentie6hc = @lc OR licentie7hc = @LC OR licentie8hc = @lc"
            + " OR licentie9hc = @lc OR licentie10hc = @lc";
            IDCheck.Parameters.AddWithValue("@id", licenseid);
            IDCheck.Parameters.AddWithValue("@lc", GetId);
    

    【讨论】:

      猜你喜欢
      • 2010-11-04
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 2014-03-14
      • 2023-03-31
      相关资源
      最近更新 更多