本文介绍下,一个mysql的例子,将查询到的数据结果保存到一个变量中。有需要的朋友可以参考下。
本代码演示:

将mysql查询结果保存到变量中的方法。

代码:

001 mysql> CREATE TABLE Employee( //创建数据表
002     ->     id            int,
003     ->     first_name    VARCHAR(15),
004     ->     last_name     VARCHAR(15),
005     ->     start_date    DATE,
006     ->     end_date      DATE,
007     ->     salary        FLOAT(8,2),
008     ->     city          VARCHAR(10),
009     ->     description   VARCHAR(15)
010     -> );
011 Query OK, 0 rows affected (0.03 sec)
012  
013 mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
014     ->              values (1,'Jason',    'Martin',  '19960725',  '20060725', 1234.56,'Toronto',  'Programmer');
015 Query OK, 1 row affected (0.00 sec)
016  
017 mysql>
018 mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
019     ->               values(2,'Alison',   'Mathews',  '19760321''19860221', 6661.78,'Vancouver','Tester');
020 Query OK, 1 row affected (0.00 sec)
021  
022 mysql>
023 mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
024     ->               values(3,'James',    'Smith',    '19781212''19900315', 6544.78,'Vancouver','Tester');
025 Query OK, 1 row affected (0.02 sec)
026  
027 mysql>
028 mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
029     ->               values(4,'Celia',    'Rice',     '19821024''19990421', 2344.78,'Vancouver','Manager');
030 Query OK, 1 row affected (0.00 sec)
031  
032 mysql>
033 mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
034     ->               values(5,'Robert',   'Black',    '19840115''19980808', 2334.78,'Vancouver','Tester');
035 Query OK, 1 row affected (0.01 sec)
036  
037 mysql>
038 mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
039     ->               values(6,'Linda',    'Green',    '19870730''19960104', 4322.78,'New York',  'Tester');
040 Query OK, 1 row affected (0.00 sec)
041  
042 mysql>
043 mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
044     ->               values(7,'David',    'Larry',    '19901231''19980212', 7897.78,'New York',  'Manager');
045 Query OK, 1 row affected (0.00 sec)
046  
047 mysql>
048 mysql> insert into Employee(id,first_name, last_name, start_date, end_Date,   salary,  City,       Description)
049     ->               values(8,'James',    'Cat',     '19960917',  '20020415', 1232.78,'Vancouver''Tester');
050 Query OK, 1 row affected (0.00 sec)
051  
052 mysql>
053 mysql> select from Employee;
054 +------+------------+-----------+------------+------------+---------+-----------+-------------+
055 | id   | first_name | last_name | start_date | end_date   | salary  | city      | description |
056 +------+------------+-----------+------------+------------+---------+-----------+-------------+
057 |    1 | Jason      | Martin    | 1996-07-25 | 2006-07-25 | 1234.56 | Toronto   | Programmer  |
058 |    2 | Alison     | Mathews   | 1976-03-21 | 1986-02-21 | 6661.78 | Vancouver | Tester      |
059 |    3 | James      | Smith     | 1978-12-12 | 1990-03-15 | 6544.78 | Vancouver | Tester      |
060 |    4 | Celia      | Rice      | 1982-10-24 | 1999-04-21 | 2344.78 | Vancouver | Manager     |
061 |    5 | Robert     | Black     | 1984-01-15 | 1998-08-08 | 2334.78 | Vancouver | Tester      |
062 |    6 | Linda      | Green     | 1987-07-30 | 1996-01-04 | 4322.78 | New York  | Tester      |
063 |    7 | David      | Larry     | 1990-12-31 | 1998-02-12 | 7897.78 | New York  | Manager     |
064 |    8 | James      | Cat       | 1996-09-17 | 2002-04-15 | 1232.78 | Vancouver | Tester      |
065 +------+------------+-----------+------------+------------+---------+-----------+-------------+
066 rows in set (0.00 sec)
067  
068 mysql> delimiter $$
069 mysql>
070 mysql> CREATE PROCEDURE myProc() //创建mysql存储过程
071     ->   READS SQL DATA
072     -> BEGIN
073     ->   DECLARE winner_id INT;
074     ->   DECLARE max_employee_id INT;
075     ->   DECLARE winner_name VARCHAR(70);
076     ->
077     ->   SELECT MAX(id)
078     ->     INTO max_employee_id
079     ->     FROM employee;
080     ->
081     ->   SET winner_id=FLOOR(RAND()*max_employee_id)+1;
082     ->
083     ->   SELECT CONCAT_WS(' ','Employee of the week is',first_name,last_name)
084     ->     FROM employee
085     ->    WHERE id=winner_id;
086     -> END$$
087 Query OK, 0 rows affected (0.00 sec)
088  
089 mysql> delimiter ;
090 mysql> call myProc(); //调用mysql存储过程
091 +---------------------------------------------------------------+
092 | CONCAT_WS(' ','Employee of the week is',first_name,last_name) |
093 +---------------------------------------------------------------+
094 | Employee of the week is James Smith                           |
095 +---------------------------------------------------------------+
096 1 row in set (0.00 sec)
097  
098 Query OK, 0 rows affected (0.00 sec)
099  
100 mysql> drop procedure myProc; //删除mysql存储过程
101 Query OK, 0 rows affected (0.00 sec)
102  
103 mysql> drop table Employee; //删除mysql数据表
104 Query OK, 0 rows affected (0.02 sec)

本文原始链接:http://www.jbxue.com/db/11778.html

mysql实例 保存查询结果到变量的更多相关文章

  1. mysql实例---sql语句中使用@变量

    本文介绍下,在mysql语句中使用@变量的一个例子,学习下这个特殊变量的用法,有需要的朋友参考下吧. 要求: 计算用户距上次访问的天数,根据imei号区分不同的用户,如果时间段内只有一次访问则为0. ...

  2. 【MySQL】分页查询实例讲解

    MySQL分页查询实例讲解 1. 前言 本文描述了团队在工作中遇到的一个MySQL分页查询问题,顺带讲解相关知识点,为后来者鉴.本文的重点不是"怎样"优化表结构和SQL语句,而是探 ...

  3. mysql查询缓存打开、设置、参数查询、性能变量意思

    http://blog.sina.com.cn/s/blog_75ad10100101by7j.html http://www.cnblogs.com/zemliu/archive/2013/08/0 ...

  4. Python操作Mysql实例代码教程在线版(查询手册)_python

    实例1.取得MYSQL的版本 在windows环境下安装mysql模块用于python开发 MySQL-python Windows下EXE安装文件下载 复制代码 代码如下: # -*- coding ...

  5. MySQL查询优化:查询慢原因和解决技巧

    在开发的朋友特别是和mysql有接触的朋友会碰到有时mysql查询很慢,当然我指的是大数据量百万千万级了,不是几十条了,下面我们来看看解决查询慢的办法. MySQL查询优化:查询慢原因和解决方法 会经 ...

  6. MySql学习(七) —— 查询性能优化 深入理解MySql如何执行查询

    本篇深入了解查询优化和服务器的内部机制,了解MySql如何执行特定查询,从中也可以知道如何更改查询执行计划,当我们深入理解MySql如何真正地执行查询,明白高效和低效的真正含义,在实际应用中就能扬长避 ...

  7. Mysql分析优化查询的方式

    一:查询语句分析 1.通过create index idx_colunmsName on tableName(columns)为某个表的某些字段创建索引,注意主键和唯一键都会自动创建索引: 如为表st ...

  8. 【原创】7. MYSQL++中的查询结果获取(各种Result类型)

    在本节中,我将首先介绍MYSQL++中的查询的几个简单例子用法,然后看一下mysqlpp::Query中的几个与查询相关的方法原型(重点关注返回值),最后对几个关键类型进行解释. 1. MYSQL++ ...

  9. MySQL安装后的设定及其变量(参数)的设置

    1.为所有root用户设定密码:mysql> SET PASSWORDmysql> update mysql.user SET password=PASSWORD("your_p ...

随机推荐

  1. AngularJS - Apply方法监听model变化

    <body> <div ng-app="myApp"> <div ng-controller="firstController" ...

  2. 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)--转

    出处:http://blog.csdn.net/xxd851116/archive/2009/06/25/4296866.aspx [前面的话] 在网上经常看到有人对request.getSessio ...

  3. Windows下用Eclipse搭建C/C++开发环境

    本文假定你已经熟悉Java,Eclipse的安装,并能顺利启动和运行Eclipse.此外因为各软件版本在不断更新,有些地方可能不准确,以最新的.原文资料为准. 距上一次写和调C++程序,已经5.6年了 ...

  4. java.lang.IllegalStateException

    java.lang.IllegalStateExceptionorg.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFac ...

  5. (浅谈).Net控件GridView绑定数据

    前台GridView属性设置 <td> <asp:GridView ID="GridView" runat="server" AutoGene ...

  6. Next Power of 2

    Next Power of 2 Write a function that, for a given no n, finds a number p which is greater than or e ...

  7. js函数大全

    js函数集·字符串(String) 1.声明 var myString = new String("Every good boy does fine."); var myStrin ...

  8. Spring(3.2.3) - Beans(1): Spring 容器

    BeanFactory & ApplicationContext org.springframework.beans.factory.BeanFactory 是最基本的 Spring 容器接口 ...

  9. Ehcache(2.9.x) - API Developer Guide, Cache Manager Event Listeners

    About CacheManager Event Listeners CacheManager event listeners allow implementers to register callb ...

  10. Git CMD - rm: Remove files from the working tree and from the index

    命令格式 git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…​ 命令参 ...