Mysql选择csv字符串中的哪个字段 [英] Mysql select where field in csv string

查看:158
本文介绍了Mysql选择csv字符串中的哪个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个表中选择一个字段,该字段位于传递给存储过程的给定csv字符串中。

I want to select from a table where a field is in a given csv string passed to a stored proc.

最快的方法是什么?应该将值插入到临时表中并加入到该表中吗?

What is the fastest way to do this? Should I insert the values into a temporary table and join to that? Or is there a way to do it with one statement?

感谢

推荐答案

新答案 -

你必须这样做,如下所示。
我假设你的csv字符串将在变量@str下面给出。否则,你需要确保你的字符串(或arraystring)应该有每个元素的单引号的这种格式 -

Well you have to do something like this as given below. I am assuming your csv string would be as given in variable @str below. Else you need to make sure that your string (or arraystring) should have this format with single quotes for every element -

set @str = "'some1','some2','some3'";
set @qry1 = CONCAT('select * from testing where col1 in (',@str,')');

prepare stmt1 from @qry1;
execute stmt1;
deallocate prepare stmt1; 

老答案 -

你将传递csv文件路径到存储过程,并读取在该存储过程中的csv行。所以基本上你可以存储所有的csv字段值在一个临时表和写查询使用IN -

I assume that you will pass the csv file path to stored proc and read the lines in csv in that stored proc. So basically you can store all those csv field values in a temp table and write query using IN -

select * from sourceTable 
where fieldValue in (select csvFieldValue from #temptable)

这篇关于Mysql选择csv字符串中的哪个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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