Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
General
Joining two Field and then Order By
ashizakp
October 2015
I would like to get joing created_at and updated_at.. and need latest by it.orderby..
possible?
Harro
October 2015
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.
ashizakp
October 2015
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.
Harro
October 2015
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.
ashizakp
October 2015
is it available with Fuel ORM?
Harro
October 2015
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..
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,089
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
261
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
ashizakp
October 2015
Harro
October 2015