第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果字段為空,則檢查記錄是否存在返回始終為假

如果字段為空,則檢查記錄是否存在返回始終為假

C#
慕田峪9158850 2021-08-22 18:02:08
所以我有以下情況:如您所見,某些字段為空,因此我想在插入記錄之前檢查表中是否已經(jīng)存在goal該記錄,我要插入的記錄包含與表中已經(jīng)可用的記錄完全相同的結(jié)構(gòu)。這是我的代碼: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());        }    }}這將返回,false但值goal是這樣的:TeamId = 95MarkerPlayer.Id = 122AssistPlaer = nullmatchId = 2564940Minute = 82'Type = 5為什么返回false?
查看完整描述

3 回答

?
慕的地6264312

TA貢獻1817條經(jīng)驗 獲得超6個贊

如果AssistPlaer是null,則不能使用=. 您需要檢查參數(shù)是否為null第一個。這是一個帶有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";

對于其他潛在null值,您可能也需要這樣做。


查看完整回答
反對 回復 2021-08-22
?
白衣非少年

TA貢獻1155條經(jīng)驗 獲得超0個贊

由于“AssistPlaer”為“NULL”,SQL 中的查詢不能使用等號運算符“=”,而必須使用“IS”或“IS NOT”與“NULL”進行比較。

您的查詢指出:

player_assist_id = @player_assist_id

但是“NULL”值不響應(yīng)相等的運算符,測試它是否為空的唯一方法是:

player_assist_id IS NULL

所以在您的查詢中,您可以使用以下內(nèi)容繞過它:

(@player_assist_id IS NULL AND player_assist_id IS NULL) OR (player_assist_id = @player_assist_id)

將此行為應(yīng)用于可以包含“NULL”的任何列。


查看完整回答
反對 回復 2021-08-22
?
元芳怎么了

TA貢獻1798條經(jīng)驗 獲得超7個贊

如果您不知道屬性值是否為 NULL,那么您可以使用 IFNULL 字符串函數(shù),以便它將 NULL 值替換為 0 或您在該特定列中定義的其他值。


查看完整回答
反對 回復 2021-08-22
  • 3 回答
  • 0 關(guān)注
  • 173 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號