Home  >  Article  >  Backend Development  >  查询二张表并输出

查询二张表并输出

WBOY
WBOYOriginal
2016-06-13 12:49:35850browse

查询2张表并输出
整个情况是这样的:
 A表结构
 id name   info
 1  栏目一  (存储样式为a:{s:6:"picid";s:2:"74";s:9:"text";s:1:"存储内容区";})
 2  栏目二  (存储样式为a:{s:6:"picid";s:2:"75";s:9:"text";s:1:"存储内容区";})
 B表结构
 id url
 74 图片路径
 首先通过查询获取到了A表info中picid的id号为74,75的数组
 输出样式为:
 Array
 (
     [0] => 74
     [1] => 75
 )
 然后在查询B表id为74,75的数据
 然后如何关联输出这2张表的内容呢?
 输出结果如何为:
 Array
 (
     [0] => Array
         (
             [id] => 1
             [name] => 栏目一
             [url] => 图片路径
             [info] => info中text的内容
         )
 
    [1] => Array
         (
             [id] => 2
             [name] => 栏目一
               [url] => 图片路径
               [info] => info中text的内容
         )
 .... 


------解决方案--------------------
取出 info 中的 text 的内容的表达式
substring_index(substring_index(substr(info, locate('"text"', info)), '"', 4), '"', -1) as info

两表的关联条件
B表.id = substring_index(substring_index(substr(info, locate('"picid"', info)), '"', 4), '"', -1)

表达式很长,你可以用存储过程来缩短他

原理很简单
a:{s:6:"picid";s:2:"74";s:9:"text";s:1:"存储内容区";} 
substr(info, locate('"picid"', info)) 用于取得 "picid" 看是的一段
"picid";s:2:"74";s:9:"text";s:1:"存储内容区";}
内层的 substring_index 用于取得 " 第4次出现以前的内容
"picid";s:2:"74
外层的 substring_index 用于取得 " 最后一次出现以后的内容
74
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn