MySQLで変数を宣言する方法

5729 ワード

How to declare a variable in MySQL?
How to declare a variable in mysql, so that my second query can use it? mysqlで変数を宣言して、2番目のクエリで使用できるようにするにはどうすればいいですか?
I would like to write something like:私はいくつかの像を書きたいです.
SET start = 1;
SET finish = 10;

SELECT * FROM places WHERE place BETWEEN start AND finish;

1階
参照先:https://stackoom.com/question/nJxF/MySQLで変数を宣言する方法
2階
There are mainly three types of variables in MySQL:MySQLには主に3種類の変数があります.
  • User-defined variables(prefixed with @):ユーザー定義変数(@で始まる):You can access any user-defined variable without declaring it or initializing.宣言や初期化を必要とせずに、任意のユーザー定義変数にアクセスできます.If you refer to a variable that has not been initialized, it has a value of NULL and a type of string. 参照される変数が初期化されていない場合、その値はNULLと文字列タイプです.
     SELECT @var_any_var_name 
    You can initialize a variable using SET or SELECT statement:SETまたはSELECT文を使用して変数を初期化できます:
     SET @start = 1, @finish = 10; 
    orまたは
     SELECT @start := 1, @finish := 10; SELECT * FROM places WHERE place BETWEEN @start AND @finish; 
    User variables can be assigned a value from a limited set of data types:integer,decimal,floating-point,binary or nonbinary string,or NULL value.ユーザー変数には、整数、10進数、浮動小数点数、バイナリまたは非バイナリ文字列またはNULL値のセットの限られたデータ型から値を割り当てることができます.User-defined variables are session-specific. ユーザー定義の変数は、セッション固有です.That is, a user variable defined by one client cannot be seen or used by other clients. すなわち、1つのクライアントによって定義されたユーザ変数は、他のクライアントによって表示または使用されない.They can be used in SELECT queries using Advanced MySQL user variable techniques . 高度なMySQLユーザー変数テクノロジーを使用して、SELECTクエリーで使用できます.
  • Local Variables(no prefix):ローカル変数(接頭辞なし):Local variables needs to be declared using DECLARE before accessing it.アクセスする前に、DECLAREを使用してローカル変数を宣言する必要があります.They can be used as local variables and the input parameters inside a stored procedure:これらは記憶プロセス内の局所変数と入力パラメータとして使用することができる:
     DELIMITER // CREATE PROCEDURE sp_test(var1 INT) BEGIN DECLARE start INT unsigned DEFAULT 1; DECLARE finish INT unsigned DEFAULT 10; SELECT var1, start, finish; SELECT * FROM places WHERE place BETWEEN start AND finish; END; // DELIMITER ; CALL sp_test(5); 
    If the DEFAULT clause is missing,the initial value is NULL.DEFAULT句が欠けている場合、初期値はNULLです.The scope of a local variable is the BEGIN ... END block within which it is declared. ローカル変数の範囲は、その内部に宣言されたBEGIN ... ENDブロックである.
  • Server System Variables(prefixed with @@):サーバシステム変数(@@接頭辞):The MySQL server maintains many system variables configured to a default value.MySQLサーバは、デフォルトに設定された多くのシステム変数を維持します.They can be of type GLOBAL , SESSION or BOTH . これらのタイプは、GLOBALSESSIONまたはBOTHであってもよい.Global variables affect the overall operation of the server whereas session variables affect its operation for individual client connections. グローバル変数はサーバ全体の操作に影響し、セッション変数は単一のクライアント接続のサーバ操作に影響します.To see the current values used by a running server, use the SHOW VARIABLES statement or SELECT @@var_name . 実行中のサーバーで使用されている現在の値を表示するには、SHOW VARIABLES文またはSELECT @@var_name文を使用します.
     SHOW VARIABLES LIKE '%wait_timeout%'; SELECT @@sort_buffer_size; 
    They can be set at server startup using options on the command line or in an option file. サーバの起動時にコマンドラインまたはオプションファイルのオプションを使用して設定できます.Most of them can be changed dynamically while the server is running using SET GLOBAL or SET SESSION:サーバがSET GLOBALまたはSET SESSIONを使用して動作する場合、ほとんどは動的に変更できます:
     -- Syntax to Set value to a Global variable: SET GLOBAL sort_buffer_size=1000000; SET @@global.sort_buffer_size=1000000; -- Syntax to Set value to a Session variable: SET sort_buffer_size=1000000; SET SESSION sort_buffer_size=1000000; SET @@sort_buffer_size=1000000; SET @@local.sort_buffer_size=10000; 
  • #3階
    Useset or select使用設定または選択
    SET @counter := 100;
    SELECT @variable_name := value;
    

    牙列缺损:
    SELECT @price := MAX(product.price)
    FROM product 
    

    #4階
    SETグループ
    SET @var_name = value 
    

    ORまたは
    SET @var := value
    

    both operators=and:=are accepted演算子=および:=が受け入れられます
    SELECT選択
    SELECT col1, @var_name := col2 from tb_name WHERE "conditon";
    

    if multiple record sets found only the last value in col2 is keep (override); 複数のレコードセットが見つかった場合、col 2の最後の値のみがkeep(上書き)である.
    SELECT col1, col2 INTO @var_name, col3 FROM .....
    

    in this case the result of select is not containing col 2 valuesこの場合、selectの結果にはcol 2値は含まれません
    #5階
    For any person using @variable in concat_ws function to get concatenated values, don't forget to reinitialize it with empty value. concat_の場合ws関数で@variableを使用して直列値を取得する人は、空の値を使用して再初期化することを忘れないでください.Otherwise it can use old value for same session. それ以外の場合、古い値を同じセッションで使用できます.
    Set @Ids = '';
    
    select 
      @Ids := concat_ws(',',@Ids,tbl.Id),
      tbl.Col1,
      ...
    from mytable tbl;
    

    #6階
    SET Value設定値
     declare Regione int;   
     set Regione=(select  id from users
     where id=1) ;
     select Regione ;