codeigniter活动记录返回指定行的列名

nfeuvbwi  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(300)

是否可以返回表中行的列名?如果是,我该怎么做?
我有一张这种结构的table

----------------------------------------------------------------------------
| idx | line1 | line2 | line3 | line1_action | line2_action | line3_action |
----------------------------------------------------------------------------
|  1  |   3   |   2   |   1   |    PENDING   |    PENDING   |   APPROVED   |
----------------------------------------------------------------------------

例如,我有一个 idx = 1 还有我的 line = 2 ,我需要检查我是否在 idx = 1 ,如何返回我所在的列名?
回答了我自己的问题

$requestIdx = $this->session->userdata('request_id');
$staffDepth = 2;
$requestInfo = $this->staffrequisition_model->selectItem(array('idx'=>$requestIdx));
foreach($requestInfo as $key=>$value){
    if(preg_match('/line\d/',$key) && $value == $staffDepth) { $lineName = $key; }
}
uinbv5nw

uinbv5nw1#

不能直接返回列名。
为此,您必须按以下方式准备查询

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

相关问题