Maison > base de données > tutoriel mysql > le corps du texte

MysqlUser-DefinedVariables用户自定义变量SETorDECLARE_MySQL

WBOY
Libérer: 2016-05-31 08:47:14
original
991 Les gens l'ont consulté

bitsCN.com 在MySQL中,我们可以将一个值或一个查询结果保存的一个用户自定义的变量中,然后在后面的语句在应用。

SET定义变量;

SET @var_name := expr [, @var_name = expr ] ....
SET @var_name = expr [, @var_name = expr ] ....
注意: ①这里用 ":=" or "="都行,但是"="在其他statement语句中有相等的意思,容易混淆,有时也会出错。强烈建议用 ":="。 ②在语句里,可以直接用@var_name = expr定义用,不提倡这样,相当于不声明直接用。

下面给出一些例子:

简单的定义,显示

mysql> <strong>SET @t1=1, @t2=2, @t3</strong>:=<strong>4;</strong>mysql> SELECT @t1, @t2, @t3, @t4 := @t1+@t2+@t3;+------+------+------+--------------------+| @t1  | @t2  | @t3  | @t4 := @t1+@t2+@t3 |+------+------+------+--------------------+|    1 |    2 |    4 |                  7 | +------+------+------+--------------------+
Copier après la connexion

Someone use them for rank

SET @pos <strong>:= </strong>0;  #这里用等号,感觉像是逻辑判断,结果也不对了SELECT @pos:=@pos+1 as rank,name FROM players ORDER BY score DESC;
Copier après la connexion

print only the 100th users

<strong>SET</strong> @counter:=0;<strong>SELECT</strong>    users.*<strong>FROM</strong>    users<strong>HAVING</strong>    (@counter:=@counter+1)%100=0<strong>ORDER BY</strong>    user_id;
Copier après la connexion

保存查询结果值value:

SELECT @total := COUNT(*)     FROM  table_name;  # simicolon 分割连个语句SELECT     table_name.id    COUNT(*) AS &#39;count&#39;,    COUNT(*) /     <strong>(SELECT         @total)</strong> AS percent FROM    table_name,WHERE 1=1GROUP BY YEAR(birthday)ORDER BY YEAR(birthday)
Copier après la connexion
注意上面这个SQL语句看起来逻辑清晰,但与下面的语句执行效果和时间都一样(可能MySQL内部优化了)
Copier après la connexion
SELECT     table_name.id    COUNT(*) AS &#39;count&#39;,    COUNT(*) /     <strong>(SELECT </strong>
Copier après la connexion
        COUNT(*) 
Copier après la connexion
     FROM 
Copier après la connexion
        table_name) AS percent 
Copier après la connexion
FROM    table_name,WHERE 1=1GROUP BY YEAR(birthday)ORDER BY YEAR(birthday)
Copier après la connexion
其他一些例子:
Copier après la connexion
http://www.mysqldiary.com/user-defined-variables/
Copier après la connexion
疑问:
Copier après la connexion
这里的变量只能保存一个结果值,如何才能临时保存一个select出的结果集呢。
Copier après la connexion
当然简单的方法是创建表/视图; 或者临时表 ,还有好的方法呢? 待研究。
Copier après la connexion

DECLARE声明变量,然后在赋值

DECLARE @var_name  var_type
Copier après la connexion
这里举个例子:
Copier après la connexion
<strong>例1:</strong>
Copier après la connexion
DECLARE @total INT DECLARE @total_distinct INTSELECT     @total:=COUNT(lice_no)    #using ":=" notationFROM    table_name;    SELECT @total_distinct:=COUNT( DISTINCT lice_no)   #using ":=" noationsFROM table_name; SELECT @total - @total_distinct
Copier après la connexion
<strong>例2:</strong>
Copier après la connexion
DECLARE @register_count INT;DECLARE @total_count INT;SELECT     @register_count := COUNT(1) FROM    t1 WHERE id > 10 ;
Copier après la connexion
    SELECT     @total_count := COUNT(1) FROM    t1 ;    SELECT     (@register_count * @total_count) AS ratio2
Copier après la connexion

DECLARE 与 SET 区别:

  1. DECLARE 必须指定类型,而SET是不用的 SET定义的是用户自定义变量,是Session Sensitive 的; DECLARE 声明的变量一般为局部变量,其有效区间是声明的函数或存储过程中。 定义全局变量应该为 SET GLOBAL @var_name 后者SET @@GLOBAL.var_name

注意及附录:

这里关于变量的使用范围要清楚。 官方介绍网址: http://dev.mysql.com/doc/refman/5.0/en/user-variables.html bitsCN.com
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!