如何在 MySQL 中模拟数组变量?
似乎 MySQL 没有数组变量.我应该用什么代替?
It appears that MySQL doesn't have array variables. What should I use instead?
似乎建议了两种替代方案:集合类型标量和临时表.我链接到的问题表明前者.但是使用这些而不是数组变量是一种好习惯吗?或者,如果我使用集合,那么与 foreach
等效的基于集合的习语是什么?
There seem to be two alternatives suggested: A set-type scalar and temporary tables. The question I linked to suggests the former. But is it good practice to use these instead of array variables? Alternatively, if I go with sets, what would be the set-based idiom equivalent to foreach
?
推荐答案
嗯,我一直在使用临时表而不是数组变量.不是最好的解决方案,但它有效.
Well, I've been using temporary tables instead of array variables. Not the greatest solution, but it works.
请注意,您不需要正式定义它们的字段,只需使用 SELECT 创建它们即可:
Note that you don't need to formally define their fields, just create them using a SELECT:
DROP TEMPORARY TABLE IF EXISTS my_temp_table;
CREATE TEMPORARY TABLE my_temp_table
SELECT first_name FROM people WHERE last_name = 'Smith';
(另请参见不使用创建表从选择语句创建临时表.)
相关文章