mysql 使用者變數不能賦予null值

2021-07-13 23:16:34 字數 2547 閱讀 7689

查詢的user_id 不為null

mysql> select  @userid :=user_id  from ta_user_info where account_name = '13870957087';

select

if(@userid is

not null , 1,0);

select @userid;

結果:

+---------------------+

| @userid :=user_id |

+---------------------+

| 5000000000001173892 |

+---------------------+

1 row in set

+-------------------------------+

| if(@userid is not null , 1,0) |

+-------------------------------+

| 1 |

+-------------------------------+

1 row in set

+---------------------+

| @userid |

+---------------------+

| 5000000000001173892 |

+---------------------+

1 row in set

從結果可以看出使用者變數@userid獲得了查詢結果,並通過if函式判斷不為null

查詢的user_id 為null

mysql> select  @userid :=user_id  from ta_user_info where account_name = '***x';

select

if(@userid is

not null , 1,0);

select @userid;

結果:

empty set

+-------------------------------+

| if(@userid is not null , 1,0) |

+-------------------------------+

| 1 |

+-------------------------------+

1 row in set

+---------------------+

| @userid |

+---------------------+

| 5000000000001173892 |

+---------------------+

1 row in set

發現使用者變數@userid確和我們想的不一樣,而是上次查詢的結果只值,而且if函式也不是null。推測是null不能賦值給使用者變數

3.查詢的user_id 不為null,換成了其他非null的值

mysql> select  @userid :=user_id  from ta_user_info where account_name = '[email protected]';

select

if(@userid is

not null , 1,0);

select @userid;

結果:

+---------------------+

| @userid :=user_id |

+---------------------+

| 5889529241746227315 |

+---------------------+

1 row in set

+-------------------------------+

| if(@userid is not null , 1,0) |

+-------------------------------+

| 1 |

+-------------------------------+

1 row in set

+---------------------+

| @userid |

+---------------------+

| 5889529241746227315 |

+---------------------+

1 row in set

可以看出使用者變數@userid又被賦予了新的值。說明第二步查詢的推論是正確的,即null不能賦值給使用者變數

所以想通過使用者變數使用if函式判斷查詢的userid是否為null 的方法也不可行。

mysql 新建使用者,賦予許可權

mysql 建立乙個使用者 hail,密碼 hail,指定乙個資料庫 haildb 給 hailmysql u root ppassworduse mysql insert into user ho mysql 建立乙個使用者 hail,密碼 hail,指定乙個資料庫 haildb 給 hail m...

mysql 建立使用者並賦予使用者許可權

1 檢視使用者 select user,host from mysql.user 返回所有使用者列表 2 建立使用者 create user username host identified by password username 使用者名稱 host 指定在哪個主機上可以登入,本機可用local...

mysql新建使用者和賦予許可權

登入mysql mysql u root p 密碼 建立使用者 insert into mysql.user host,user,password values localhost hbchen p assword hbchen 這樣就建立了乙個名為 hbchen 密碼為 hbchen 的 使用者。...