如何使用mysql将查询结果存储在variables中
SET @v1 := SELECT COUNT(*) FROM user_rating; SELECT @v1
当我用set
variables执行这个查询时,显示这个错误。
Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT count(*) FROM user_rating' at line 1 Execution Time : 00:00:00:000 Transfer Time : 00:00:00:000 Total Time : 00:00:00:000 (1 row(s) returned) Execution Time : 00:00:00:343 Transfer Time : 00:00:00:000 Total Time : 00:00:00:343
用圆括号select的环绕声。
SET @v1 := (SELECT COUNT(*) FROM user_rating); SELECT @v1;
此外,如果您想通过一个查询一次设置多个variables,则可以使用其他语法来设置variables,如下所示: SELECT @varname:=value
。
一个实际的例子:
SELECT @total_count:=COUNT(*), @total_price:=SUM(quantity*price) FROM items ...
用这个
SELECT weight INTO @x FROM p_status where tcount=['value'] LIMIT 1;
testing和工作很好…