Wednesday 15 July 2015

php - Drupal db_select PDO Statement: ORDER BY CASE -


Hello I have written a MySQL statement that contains a conditional order, which you can see in the example below.

MySQL example:

  Choose title, description from books, title or description "% keyword%" such as title "% keyword%" When the "% keyword%" like the order title is then 1 ELSE 2 END   

I am now trying to recreate this statement in Drupal using its PDO style db_select () function. I am But I'm stuck while writing the ORDER BY clause.

Druid Example:

  $ node_select = db_select ('node', 'n'); $ Node_select- & gt; Field ('n', array ('title', 'description')); $ Node_select- & gt; Condition (db_or () - & gt; Condition ('n.title', '%' Keywords. '%', 'Like') - & gt; Condition ('n.description', '%' $ keyword. ' %', 'like')); $ Node_select- & gt; order ();   

Does anyone point me in the right direction how would you write the ORDER BY section?

I think you can select it by adding an expression to the statement, then adding that field Order by region will only work if you are correcting additional fields in the selection list. This will do something like this:

  $ query-> addExpression ('case when the title will be like'% keyword% 'then 1 ELSE 2 END', 'order_col'); $ Query-> ; By command ('order_col', 'ASC');   

I do not think you can use expression specifically in sequence, but it should work for you .

No comments:

Post a Comment