我在这里用google搜索了两个小时后在这里问了一个问题,并在这里搜索了很多。但10多分钟后,我发现我已经得到了答案......
在select()方法的源代码服用后一看,
public function select($columns='*', $option='')
{
if(is_string($columns) && strpos($columns,'(')!==false)
$this->_query['select']=$columns;
else
{
if(!is_array($columns))
$columns=preg_split('/\s*,\s*/',trim($columns),-1,PREG_SPLIT_NO_EMPTY);
foreach($columns as $i=>$column)
{
if(is_object($column))
$columns[$i]=(string)$column;
else if(strpos($column,'(')===false)
{
if(preg_match('/^(.*?)(?i:\s+as\s+|\s+)(.*)$/',$column,$matches))
$columns[$i]=$this->_connection->quoteColumnName($matches[1]).' AS '.$this->_connection->quoteColumnName($matches[2]);
else
$columns[$i]=$this->_connection->quoteColumnName($column);
}
}
$this->_query['select']=implode(', ',$columns);
}
if($option!='')
$this->_query['select']=$option.' '.$this->_query['select'];
return $this;
}
,请注意第一个if语句,当一个字符串传递,它包含“(”时,$列变量将直接返回,这真的是我期待的
因此,解决办法是:!
$sql->select('A.name as client_name, (CASE WHEN B.name = "sth" THEN B.name ELSE B.product_name END) as product_name');
要小心的是,其他外作为product_name“的部分应该超出()部分。
有时候自己比搜索引擎有用:) –
谢谢,金招。可能花了我一些时间来解决这个问题,但是多亏了你,我花了一段时间在Google上打字并点击了这个链接。 –
@RichHarding很高兴帮助你:) –