2016-09-23 59 views
1

我打算把一個查詢放到我的變量中,但是我的問題是我需要將不同的一組值分開以將它們分離到我的PHP輸出中我想知道這是否可能,甚至對SQL查詢的幫助也是一大幫助。見我的截圖: enter image description here分離表中的行的值

如果你看一下表section柱的底部,你會看到有一個GI21GI122。現在,當我現在使用的查詢是這樣的。

SELECT `scstock`.*, `schead`.* FROM `scstock` LEFT JOIN `schead` ON schead.TrNo = scstock.TrNo WHERE (`schead`.`curriculumcode`='BSIT 2011') AND (`schead`.`styear`='4') AND (`schead`.`terms`='1ST') AND (`schead`.`isBlock`=1) 

或Yii中

->select(['scstock.*', 'schead.*']) 
    ->leftJoin('schead', 'schead.TrNo = scstock.TrNo') 
    ->where(['schead.curriculumcode' => $curriculumcode, 
       'schead.styear' => $year, 
       'schead.terms' => $term, 
       'schead.isBlock' => 1 
       ]) 
    ->asArray() 
    ->all(); 

將輸出的所有行已isBlock = 1但我需要做的是分離GI21GI122,也許把它們放入數組一樣,但現在可能有一個更好的實現,但現在將它們放入一個數組後,我將它們返回到像這樣的表中放入視圖中。

<table class="table table-bordered" id="studentTable"> 
       <th>Subject</th> 
       <th>Schedule</th> 
       <th>Section</th> 
       <th>Action</th> 
       <th>Slots</th> 
       <th>Status</th> 
      <?php foreach($getBlock as $values): ?> 

        <tr> 
        <td><?= $values['subjectcode']; ?></td> 
        <td><?= $values['schedday'] . ' ' . $values['schedtime'] ?></td> 
        <td><?= $values['section'] ?></td> 
        <td><?= '....' ?></td> 
        <td><?= $values['slots'] ?></td> 
        <td><?= '....' ?></td> 
        </tr> 

      <?php endforeach; ?> 
       </table> 

我們在視圖中他們分開,也許我只是把foreachtable標籤的部分分開? 如何分組不同的值以將它們分離到我的PHP輸出中?任何形式的幫助,將不勝感激。

回答

1

你可以使用兩個單獨的查詢一個不(G121和GI122),例如:getBlock1

->select(['scstock.*', 'schead.*']) 
    ->leftJoin('schead', 'schead.TrNo = scstock.TrNo') 
    ->where(['schead.curriculumcode' => $curriculumcode, 
       'schead.styear' => $year, 
       'schead.terms' => $term, 
       'schead.isBlock' => 1, 
       'not in','schead.section',['G121', 'GI122'], 
       ]) 
    ->asArray() 
    ->all(); 

和第二與(G121和GI122),例如:getBlock2

where(['not in','attribute',$array]); 



->select(['scstock.*', 'schead.*']) 
    ->leftJoin('schead', 'schead.TrNo = scstock.TrNo') 
    ->where(['schead.curriculumcode' => $curriculumcode, 
       'schead.styear' => $year, 
       'schead.terms' => $term, 
       'schead.isBlock' => 1, 
       'in','schead.section',['G121', 'GI122'], 
       ]) 
    ->asArray() 
    ->all(); 

的,你可以在第一個foreach中關聯到$ getBlock1,在第二個foreach中關聯到$ getBlock2