Mysql多重addwithvalue:一个返回值就足够了

本文关键字:返回值 一个 addwithvalue 多重 Mysql | 更新日期: 2023-09-27 18:03:17

我有一个查询,有多个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"answers"id"匹配=>返回true

或者我们说只有"id"answers"licentie1hc"匹配。=>返回true

这可能吗?我知道我可以使用不同的查询,但我只需要"id"和一个"许可证x hc"参数匹配…

Mysql多重addwithvalue:一个返回值就足够了

试试这个:

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);

注意:我知道你可能不能改变你的数据库结构,但是你有十列,表明你的数据库可以从一些重构中受益。

改变你的查询使用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);