Thursday 15 March 2012

sql - orderBy in UNION query Giving SQLSTATE[42601] error -



sql - orderBy in UNION query Giving SQLSTATE[42601] error -

note: using lravel 4.1 framework

hi sql query postgres db

$results1 = db::table('vehicle_brands') ->where('vehicle_brands.caption','ilike', '%'.$_get['key_word'].'%') ->join('vehicle_manufacturers','vehicle_manufacturers.id', '=', 'vehicle_brands.vehicle_manufacturer_id') ->select('vehicle_brands.*','vehicle_manufacturers.caption vm_caption'); $results = db::table('vehicle_brands') ->where('vehicle_manufacturers.caption','ilike', '%'.$_get['key_word'].'%') ->join('vehicle_manufacturers','vehicle_manufacturers.id', '=', 'vehicle_brands.vehicle_manufacturer_id') ->select('vehicle_brands.*','vehicle_manufacturers.caption vm_caption') ->union($results1) ->orderby('vehicle_brands.caption','asc') ->get(); homecoming response::json(array('status' => true,'data' => $results));

the error getting follows:

sqlstate[42601]: syntax error: 7 error: syntax error @ or near "union" line 1: ... ilike $1 order "vehicle_brands"."caption" asc union sele... ^ (sql: select "vehicle_brands"., "vehicle_manufacturers"."caption" "vm_caption" "vehicle_brands" inner bring together "vehicle_manufacturers" on "vehicle_manufacturers"."id" = "vehicle_brands"."vehicle_manufacturer_id" "vehicle_manufacturers"."caption" ilike %o% order "vehicle_brands"."caption" asc union select "vehicle_brands"., "vehicle_manufacturers"."caption" "vm_caption" "vehicle_brands" inner bring together "vehicle_manufacturers" on "vehicle_manufacturers"."id" = "vehicle_brands"."vehicle_manufacturer_id" "vehicle_brands"."caption" ilike %o%)

what doing wrong in query?

thankyou.

i using laravel 4.1 framework , postgres db.

i researched farther after posting above question , came solution question follows.

it bug give-and-take can seen @ https://github.com/laravel/framework/pull/3901 give-and-take provides solution too.

found stackoverflow solution too(this same above clarity)

http://stackoverflow.com/a/25722206

i coverted code below :

$query1 = db::table('vehicle_brands') ->where('vehicle_brands.caption','ilike', '%'.$term.'%') ->join('vehicle_manufacturers','vehicle_manufacturers.id', '=', 'vehicle_brands.vehicle_manufacturer_id') ->select('vehicle_brands.*','vehicle_manufacturers.caption vm_caption'); $query2 = db::table('vehicle_brands') ->where('vehicle_manufacturers.caption','ilike', '%'.$term.'%') ->join('vehicle_manufacturers','vehicle_manufacturers.id', '=', 'vehicle_brands.vehicle_manufacturer_id') ->select('vehicle_brands.*','vehicle_manufacturers.caption vm_caption'); $query = $query1->union($query2); $items = db::table(db::raw("($querysql order 'caption' 'asc') a"))->mergebindings($query) ->take($limit) ->skip($limit * ($page - 1)) ->get();

the $term search keyword.

thank you.

sql postgresql laravel-4 union

No comments:

Post a Comment