请教sql查询2张表的问题:
a表为
id title picid
其中picid存储信息为
a:4:{s:6:attach;s:2:74;s:9:show_type;s:1:0;s:9:user_bind;s:1:1;s:10:topic_bind;s:0:;}
b表为
p_id purl
我首先通过查询a表获得了picid字段中的attach数据,输出为数组形式为
aryy{
【0】74
【1】88
....
}
那么我如何通过查询a表得到的attach去查询b表 p_id 为74 88的匹配id呢?
能给个代码解释下吗?
回复讨论(解决方案) 设第一步得到的数组为
$ar = array(74, 88);
则有
$sql = 'select * from b表 where p_id in (' . join(',', $ar) . ')';
如果 picid 中 attach 出现的次序是固定的话,还可以直接关联两表查询
比如
select p_id, purl from b表, a表 where p_id = substring_index(substring_index(picid, 4), '', -1)
设第一步得到的数组为
$ar = array(74, 88);
则有
$sql = 'select * from b表 where p_id in (' . join(',', $ar) . ')';
如果 picid 中 attach 出现的次序是固定的话,还可以直接关联两表查询
如果出现次序不是固定的呢?能关联两表查询吗
用 locate 函数先定位,应该也是可以的