You can employ MySQL user variables to remember results without having to store them in temporary variables in the client. (See Section 9.4, “User-Defined Variables”.)您可以使用MySQL用户变量来记住结果,而不必将它们存储在客户端的临时变量中。(参见第9.4节,“用户定义变量”。)
For example, to find the articles with the highest and lowest price you can do this:例如,要查找价格最高和最低的文章,可以执行以下操作:
mysql>SELECT @min_price:=MIN(price),@max_price:=MAX(price) FROM shop;
mysql>SELECT * FROM shop WHERE price=@min_price OR price=@max_price;
+---------+--------+-------+ | article | dealer | price | +---------+--------+-------+ | 0003 | D | 1.25 | | 0004 | D | 19.95 | +---------+--------+-------+
It is also possible to store the name of a database object such as a table or a column in a user variable and then to use this variable in an SQL statement; however, this requires the use of a prepared statement. 也可以将数据库对象(如表或列)的名称存储在用户变量中,然后在SQL语句中使用该变量;但是,这需要使用事先准备好的语句。See Section 13.5, “Prepared Statements”, for more information.有关更多信息,请参阅第13.5节,“Prepared语句”。