是否可以返回表中某行的列名?如果是,我该怎么做?
我有一张这种结构的桌子
----------------------------------------------------------------------------
| idx | line1 | line2 | line3 | line1_action | line2_action | line3_action |
----------------------------------------------------------------------------
| 1 | 3 | 2 | 1 | PENDING | PENDING | APPROVED |
----------------------------------------------------------------------------
例如,我有一个idx = 1
和我的line = 2
,我需要检查我是否在idx = 1
的第1、2或3行,我如何返回我所在的列名?
回答了我自己的问题
$requestIdx = $this->session->userdata('request_id');
$staffDepth = 2;
$requestInfo = $this->staffrequisition_model->selectItem(array('idx'=>$requestIdx));
foreach($requestInfo as $key=>$value){
if(preg_match('/lined/',$key) && $value == $staffDepth) { $lineName = $key; }
}
不能直接返回列名。
为此,您必须准备如下方式的查询
SELECT idx, IF(line1 = 2, 'line1', if(line2 = 2, 'line2', line3 = 3, 'line3', '')) AS line, line2, line3 FROM table_name WHERE (line1 = 2 OR line2 = 2 OR line3 = 2) AND idx = 1