MySQL在创建多维数组后离开了join

时间:2014-11-18 22:44:06

标签: php mysql arrays multidimensional-array left-join

我有2个表,外键是' timesection_id'。 第一张表:时间段


| id |名字|时间|职位|


第二个表格是内容


| id |标题| short_desc | desc | img |位置| timesection_id |

我想创建一个左连接到第一个表。它必须按位置排序。 结果应该是这样的:

array (size=1)
  'content' => 
    array (size=2)
      1 => 
        array (size=3)
          'time' => string '12-00' (length=5)
          'name' => string 'Start' (length=21)
          'performer' => 
            array (size=3)
                   0=>
                        'title' => string '12-00'
                        'desc' => string 'etc etc'
                        'shor_desc' => string 'etc etc'
                        'img' => string '1.jpg'
                   1=>
                        'title' => string '12-00'
                        'desc' => string 'etc etc'
                        'shor_desc' => string 'etc etc'
                        'img' => string '1.jpg'
      2 => 
        array (size=3)
          'time' => string '13-00' (length=5)
          'name' => string 'Something' (length=24)
          'performer' => 
            array (size=3)
                   0=>
                        'title' => string '12-00'
                        'desc' => string 'etc etc'
                        'shor_desc' => string 'etc etc'
                        'img' => string '1.jpg'
                   1=>
                        'title' => string '12-00'
                        'desc' => string 'etc etc'
                        'shor_desc' => string 'etc etc'
                        'img' => string '1.jpg'

我的sql:

SELECT * FROM timesection
    LEFT OUTER JOIN content ON  timesection_id = timesection.id
    ORDER BY timesection.position ASC

我有这个结果:

 0 => 
    array (size=9)
      'id' => null
      'name' => string 'KezdĂŠs' (length=7)
      'time' => string '11:00' (length=5)
      'position' => null
      'title' => null
      'short_desc' => null
      'desc' => null
      'img' => null
      'timesection_id' => null
  1 => 
    array (size=9)
      'id' => string '1' (length=1)
      'name' => string 'KezdĂŠs, Sikerdal debĂźtĂĄlĂĄsa' (length=31)
      'time' => string '12:00-14.00' (length=11)
      'position' => string '1' (length=1)
      'title' => string 'Ăv Embere orvoscsoport' (length=23)
      'short_desc' => string '' (length=0)
      'desc' => string 'PĂŠldĂĄtlan ĂśsszefogĂĄs eredmĂŠnyekĂŠnt egĂŠszsĂŠges gyermeket szĂźlt ĂŠs nĂŠgy ĂŠletet mentett meg egy tĂśbb hĂłnapja agyhalott asszony Debrecenben' (length=149)
      'img' => string '1.jpg' (length=5)
      'timesection_id' => string '2' (length=1)
  2 => 
    array (size=9)
      'id' => string '2' (length=1)
      'name' => string 'KezdĂŠs, Sikerdal debĂźtĂĄlĂĄsa' (length=31)
      'time' => string '12:00-14.00' (length=11)
      'position' => string '2' (length=1)
      'title' => string 'Varga RĂłbert' (length=13)
      'short_desc' => string '' (length=0)
      'desc' => string 'Varga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbert' (length=156)
      'img' => string '1.jpg' (length=5)
      'timesection_id' => string '2' (length=1)
  3 => 
    array (size=9)
      'id' => string '3' (length=1)
      'name' => string 'KezdĂŠs, Sikerdal debĂźtĂĄlĂĄsa' (length=31)
      'time' => string '12:00-14.00' (length=11)
      'position' => string '3' (length=1)
      'title' => string 'BenkĹ Vilmos ĂŠs fiatal tehetsĂŠgek' (length=36)
      'short_desc' => string '' (length=0)
      'desc' => string 'BenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgek' (length=396)
      'img' => string '1.jpg' (length=5)
      'timesection_id' => string '2' (length=1)

如果主表有更多来自子表的记录,我想创建一个执行者值并将其放在子数组中。性能值可以为空。

1 个答案:

答案 0 :(得分:2)

SQL查询:

SELECT timesection.id,timesection.name,timesection.time,
   content.title,content.short_desc,content.desc,content.img,content.id as cid
   FROM timesection
   LEFT JOIN content ON content.timesection_id = timesection.id
   ORDER BY timesection.position , content.position

PHP代码:(注意:使用旧的弃用的mysql_ *函数,请考虑使用mysqli或PDO)

$r = mysql_query($query);
$array = array('content'=>array());
$i=0;
$lastid = null;
while($row=mysql_fetch_object($r))
{
 if($lastid!==$row->id)
 {
  $array['content'][++$i]=array('time'=>$row->time,'name'=>$row->name,'performer'=>array());
  $lastid=$row->id;
 }
 if($row->cid!==null)
 {
  $array['content'][$i]['performer'][]=array('title'=>$row->title,'short_desc'=>$row->short_desc,'desc'=>$row->desc,'img'=>$row->img);
 }
}