mysql将数据传递到 [英] mysql passing data to in

查看:106
本文介绍了mysql将数据传递到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想从tblvw1中获取一些数据.另一个表在作为连接字符串存储的列中包含可能的id,例如:"1 | 2 | 3 | 4". 接下来,我尝试通过以下查询获取结果:

Basically I want to fetch some data from tblvw1. Another table contains possible ids within a column which is stored as concatenated string, for example: "1|2|3|4". Next I have tried to get the result by the following query:

SELECT x FROM tblname1 
WHERE id IN (SELECT REPLACE(content,'|',',') FROM tblname2 WHERE dataid = y )

作为结果,我仅获得第一个值,而未获取其他数据. 我想子查询将以这种形式生成:

AS result I only get the first value, the other data wasn't fetched. I suppose that the subquery will result in this form:

SELECT x FROM tblname1 WHERE id IN ('1,2,3,4')  

但我想将此子查询设置为

but i want to have this subquery either as

 SELECT x FROM tblname1 WHERE id IN (1,2,3,4)  

  SELECT x FROM tblname1 WHERE id IN ('1','2','3','4')  

有什么想法吗?

推荐答案

可以使用find_in_set函数吗?

SELECT x FROM tblname1 t1
inner join tblname2 t2 on find_in_set (t1.id, REPLACE(t2.content,'|',',')) > 0
where t2.dataid = 'y';

find_in_set函数返回第二个参数中第一个参数的位置.如果结果> 0,则找到第一个参数.

The find_in_set function returns the position of the first argument within the second argument. If the result is >0, then the first argument has been found.

请参见 http://dev.mysql.com/doc/refman/5.7/zh-CN/string-functions.html#function_find-in-set

这篇关于mysql将数据传递到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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