mysql实例 select into设置变量的方法

发布时间:2020-03-11编辑:脚本学堂
本文介绍下,在mysql中,使用select ... into 设置变量的方法,有需要的朋友,可以作个参考。

mysql实例,select into设置变量,代码:

mysql> CREATE TABLE Employee( //创建雇员表
    ->     id            int,
    ->     first_name    VARCHAR(15),
    ->     last_name     VARCHAR(15),
    ->     start_date    DATE,
    ->     end_date      DATE,
    ->     salary        FLOAT(8,2),
    ->     city          VARCHAR(10),
    ->     description   VARCHAR(15)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
    ->              values (1,'Jason',    'Martin',  20060725',  '20060725', 1234.56, 'Toronto',  'Programmer');
Query OK, 1 row affected (0.01 sec)

mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
    ->               values(2,'Alison',   'Mathews',  '20060321', '20060221', 6661.78, 'Vancouver','Tester');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
    ->               values(3,'James',    'Smith',    '20081212', 20000315', 6544.78, 'Vancouver','Tester');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
    ->               values(4,'Celia',    'Rice',     '20021024', 20090421', 2344.78, 'Vancouver','Manager');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
    ->               values(5,'Robert',   'Black',    '20040115', 20080808', 2334.78, 'Vancouver','Tester');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
    ->               values(6,'Linda',    'Green',    '20070730', 20060104', 4322.78,'New York',  'Tester');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
    ->               values(7,'David',    'Larry',    20001231', 20080212', 7897.78,'New York',  'Manager');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
    ->               values(8,'James',    'Cat',     20060917',  '20020415', 1232.78,'Vancouver', 'Tester');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> select * from Employee; //查询雇员数据
+------+------------+-----------+------------+------------+---------+-----------+-------------+
| id   | first_name | last_name | start_date | end_date   | salary  | city      | description |
+------+------------+-----------+------------+------------+---------+-----------+-------------+
|    1 | Jason      | Martin    | 2006-07-25 | 2006-07-25 | 1234.56 | Toronto   | Programmer  |
|    2 | Alison     | Mathews   | 2006-03-21 | 2006-02-21 | 6661.78 | Vancouver | Tester      |
|    3 | James      | Smith     | 2008-12-12 | 2000-03-15 | 6544.78 | Vancouver | Tester      |
|    4 | Celia      | Rice      | 2002-10-24 | 2009-04-21 | 2344.78 | Vancouver | Manager     |
|    5 | Robert     | Black     | 2004-01-15 | 2008-08-08 | 2334.78 | Vancouver | Tester      |
|    6 | Linda      | Green     | 2007-07-30 | 2006-01-04 | 4322.78 | New York  | Tester      |
|    7 | David      | Larry     | 2000-12-31 | 2008-02-12 | 7897.78 | New York  | Manager     |
|    8 | James      | Cat       | 2006-09-17 | 2002-04-15 | 1232.78 | Vancouver | Tester      |
+------+------------+-----------+------------+------------+---------+-----------+-------------+
8 rows in set (0.00 sec)

mysql> drop table Employee; //删除雇员表
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> Set @myInt = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT COUNT(*) INTO @myInt FROM employee; //统计结果存入变量myInt中,select into的例子
ERROR 1146 (42S02): Table 'test.employee' doesn't exist
mysql> select @myInt;
+--------+
| @myInt |
+--------+
| 0      |
+--------+
1 row in set (0.00 sec)

mysql>
--You can also select multiple values into multiple variables:
SELECT id,name INTO new_id,new_name FROM employee LIMIT 1;