Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I asked the question here after struggling for 2 hours by googled and searched a lot here. But 10 minutes later, I found I've got the answer...</p> <p>After taking a look at the source code of select() method, </p> <pre><code>public function select($columns='*', $option='') { if(is_string($columns) &amp;&amp; strpos($columns,'(')!==false) $this-&gt;_query['select']=$columns; else { if(!is_array($columns)) $columns=preg_split('/\s*,\s*/',trim($columns),-1,PREG_SPLIT_NO_EMPTY); foreach($columns as $i=&gt;$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-&gt;_connection-&gt;quoteColumnName($matches[1]).' AS '.$this-&gt;_connection-&gt;quoteColumnName($matches[2]); else $columns[$i]=$this-&gt;_connection-&gt;quoteColumnName($column); } } $this-&gt;_query['select']=implode(', ',$columns); } if($option!='') $this-&gt;_query['select']=$option.' '.$this-&gt;_query['select']; return $this; } </code></pre> <p>Pay attention to the 1st if statement, when a string is passed and it contains "(", the $columns variable will be returned directly, that's really what I am looking for! </p> <p>So the solution will be:</p> <pre><code>$sql-&gt;select('A.name as client_name, (CASE WHEN B.name = "sth" THEN B.name ELSE B.product_name END ) as product_name'); </code></pre> <p>Take care that the alias part "as product_name" should be out of () section.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload