Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Joining two Field and then Order By
  • I would like to get joing created_at and updated_at.. and need latest by it.orderby..

    possible?
  • On a DB call you mean?

    You can use multiple order_by's, so you can do

    ...->order_by('fieldA', 'ASC')->order_by('fieldB', 'DESC')->execute();

    Which would order first on fieldA, and within that, on fieldB.
  • Sorry,

    i need to get the last updated by orderby, it include both created_at and updated_at. so i need sort the merged list of both.
  • You mean you want to order on the highest value in both fields, so you have the one that is created yesterday below the one that is updated today?

    If so, have a look at GREATEST() in MySQL. You can use it to select the biggest value from multiple columns, and order on that.
  • is it available with Fuel ORM?
  • No, you will have to use DB::expr(), but I've never tried that in an order_by().

    Something like:

    ->order_by(\DB::expr("GREATEST(fieldA, fieldB)"), 'ASC')->

    Or if a field can contain NULL, use

    ->order_by(\DB::expr("GREATEST(COALESCE(fieldA, 0), COALESCE(fieldB, 0))"), 'ASC')->

    NB: This is MySQL specific, so if you use another database, you may have to find the correct syntax for your RDBMS..

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion