[英]Check if a record exists return always false if the fields is null
So I have the following situation: 所以我有以下情况:
as you can see some fields are null, so I want check before insert the record is this already exists inside the table goal
, the record that I'm going to insert contains exactly the same structure of the record already avaialble in the table. 如您所见,某些字段为null,因此我想在插入记录之前检查该数据是否已经存在于表
goal
,我要插入的记录包含与表中已经可用的记录结构完全相同的结构。
This is my code: 这是我的代码:
public bool CheckGoalExist(Goal goal, Goal.GoalType type, int matchId)
{
using (MySqlConnection connection = new DBConnection().Connect())
{
using (MySqlCommand command = new MySqlCommand())
{
command.Connection = connection;
command.CommandText = "SELECT COUNT(*) FROM goal " +
"WHERE player_marker_id = @player_marker_id AND " +
"team_id = @team_id AND " +
"player_assist_id = @player_assist_id AND " +
"match_id = @match_id AND " +
"minute = @minute AND " +
"type = @type";
command.Parameters.AddWithValue("@team_id", goal.TeamId);
command.Parameters.AddWithValue("@player_marker_id", goal.MarkerPlayer.Id);
command.Parameters.AddWithValue("@player_assist_id", goal.AssistPlayer?.Id);
command.Parameters.AddWithValue("@match_id", matchId);
command.Parameters.AddWithValue("@minute", goal.Minute);
command.Parameters.AddWithValue("@type", GetGoalTypeId(type));
return Convert.ToBoolean(command.ExecuteScalar());
}
}
}
this will return false
but the value of goal
are this: 这将返回
false
但goal
是:
TeamId = 95
MarkerPlayer.Id = 122
AssistPlaer = null
matchId = 2564940
Minute = 82'
Type = 5
why return false? 为什么返回假?
If AssistPlaer
is null
, then you cannot use =
. 如果
AssistPlaer
为null
,则不能使用=
。 You need to check if the parameter is null
first. 您需要首先检查参数是否为
null
。 Here's a common approach with an or
statement: 这是带有
or
语句的常见方法:
command.CommandText = "SELECT COUNT(*) FROM goal " +
"WHERE player_marker_id = @player_marker_id AND " +
"team_id = @team_id AND " +
"(@player_assist_id is null or player_assist_id = @player_assist_id) AND " +
"match_id = @match_id AND " +
"minute = @minute AND " +
"type = @type";
You may need to do that for other potential null
values as well. 您可能还需要对其他潜在的
null
值执行此操作。
since "AssistPlaer" is "NULL", the query in SQL cannot use the equal operator "=", but must compare to "NULL" using the "IS" or "IS NOT". 由于“ AssistPlaer”为“ NULL”,SQL中的查询不能使用等于运算符“ =”,而必须使用“ IS”或“ IS NOT”与“ NULL”进行比较。
your query states: 您的查询指出:
player_assist_id = @player_assist_id
but "NULL" values do not respond to equal operators, the only way to test if it's null or not is this: 但是“ NULL”值不会响应相等的运算符,因此测试它是否为null的唯一方法是:
player_assist_id IS NULL
so in your query you could bypass that using something like: 因此在您的查询中,您可以使用类似以下内容的方法绕过该方法:
(@player_assist_id IS NULL AND player_assist_id IS NULL) OR (player_assist_id = @player_assist_id)
apply this behavior to any column that can contain "NULL". 将此行为应用于可以包含“ NULL”的任何列。
如果您不知道该属性值是否为NULL,则可以使用IFNULL字符串函数,以便它将NULL值替换为0或您在该特定列中定义的某个其他值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.