簡體   English   中英

創建多維數組后,MySQL左聯接

[英]MySQL left join, after create a multidimensional array

我有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)

如果主表有更多來自子表的記錄,我想創建一個執行者值並將其放在子數組中。 Performen值可以為null。

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);
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM