• 技术文章 >数据库 >mysql教程

    MySQL – Introduction to User Defined Variables_MySQL

    2016-06-01 13:12:29原创474
    MySQL supports user defined variables to have some data that can be used later part of your query. You can save a value to a variable using a SELECT statement and later you can access its value.

    Unlike other RDBMSs, you do not need to declare the data type for a variable. The data type is automatically assumed when you assign a value. A value can be assigned to a variable using a SET command as shown below

    SET @server_type:='MySQL';

    When you above command is executed, the value, MySQL is assigned to the variable called @server_type. Now you can use this variable in the later part of the code. Suppose if you want to display the value, you can use SELECT statement.

    SELECT @server_type;

    The result is MySQL. Once the value is assigned it remains for the entire session until changed by the later statements. So unlike SQL Server, you do not need to have this as part the execution code every time. (Because in SQL Server, the variables are execution scoped and dropped after the execution).

    You can give column name as below

    SELECT @server_type AS server_type;

    You can also SELECT statement to DECLARE and SELECT the values for a variable.

    SELECT @message:='Welcome to MySQL' AS MESSAGE;

    The result is

    Message --------Welcome to MySQL

    You can make use of variables to effectively apply many logics. One of the useful method is to generate the row number as shown in this postMySQL – Generating Row Number for Each Row using Variable.

    Reference: Pinal Dave (http://blog.sqlauthority.com)

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:
    上一篇:尚客:MYSQL安全之删除历史操作文件_MySQL 下一篇:MySql基本语法(学习笔记)_MySQL
    20期PHP线上班

    相关文章推荐

    精选22门好课,价值3725元,开通VIP免费学习!• mysql查询慢的因素除了索引,还有什么?• 一文掌握Mysql中的Enum数据类型• MySQL读写分离基本原理详解• 浅析mysql设置最大连接数的两种方法• 实例解析MySQL约束知识点
    1/1

    PHP中文网