2016-09-23 61 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