我得到了一个从 1 到 1000 的 id 表。如何从表中选择行并将结果插入到我声明的两个变量中?
像这样的一些事情:
Select from table1 where id = 1 or id = 27 into var1, var2
SELECT
@var1 := MAX(IF(id = 1, some_field, NULL)),
@var2 := MAX(IF(id = 27, some_field, NULL))
FROM table
WHERE id IN (1,27);
您可以这样做,但您需要确认以下两个限制:
看到这个:
SELECT 'foo', 4 FROM DUAL INTO @a, @b;
SELECT @a, @b;
+------+------+
| @a | @b |
+------+------+
| foo | 4 |
+------+------+
还有可能:
SELECT MIN(id), MAX(Population) FROM world.City INTO @min_id, @max_pop;
SELECT name, population FROM world.Country LIMIT 1 INTO @name, @pop;