如何使用mysql中的数组循环? [英] How to cycle with an array in mysql?

查看:4952
本文介绍了如何使用mysql中的数组循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建或存储过程或与阵列上传递的值正常查询。

I'd like to create or a stored procedure or a normal query with values passed on a array. Ex.

CREATE PROCEDURE proc() 
BEGIN 
    DECLARE cont INTEGER; 
    DECLARE var ARRAY; 
    SET cont = 0;
    SET var = ("hi", "hello", "good", ...)

    WHILE cont < 12 DO 
        SELECT * FROM tablex
        WHERE name = var[cont];
        SET cont = cont + 1; 
    END WHILE; 
END;

这显然是错误的,但我想知道如何实现这一任务。

谢谢你在前进,

 戴夫

Obviously this is wrong but I'd like to know how to achieve this task.
Thank you in advance,
Dave

由于可以在PHP或任何编程语言:

As could be in php or in any programming language:

x = new Array("a", "b", "c");
for (int i = 0; i < x.length; i++){
    result = DO_SELECT_WITH_ARGUMENT(x[i]);
}

可能是这样的......但在MySQL只。

Could be something like this...but in mysql only.

推荐答案

试着做没有存储程序 -

Try to do it without stored routine -

SET @arr = 'hi,hello,good'; -- your array

SELECT COUNT(*) FROM tablex
  WHERE FIND_IN_SET (name, @arr); -- calculate count

这篇关于如何使用mysql中的数组循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆