注意:在使用Lravel 4.1框架排序依據聯合查詢中給予SQLSTATE [42601]錯誤
你好我的SQL查詢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 AS 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 AS vm_caption')
->union($results1)
->orderBy('vehicle_brands.caption','ASC')
->get();
return Response::json(array('status' => true,'data' => $results));
我得到的錯誤如下:
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "union" LINE 1: ... ilike $1 order by "vehicle_brands"."caption" asc union sele...^(SQL: select "vehicle_brands"., "vehicle_manufacturers"."caption" as "vm_caption" from "vehicle_brands" inner join "vehicle_manufacturers" on "vehicle_manufacturers"."id" = "vehicle_brands"."vehicle_manufacturer_id" where "vehicle_manufacturers"."caption" ilike %o% order by "vehicle_brands"."caption" asc union select "vehicle_brands"., "vehicle_manufacturers"."caption" as "vm_caption" from "vehicle_brands" inner join "vehicle_manufacturers" on "vehicle_manufacturers"."id" = "vehicle_brands"."vehicle_manufacturer_id" where "vehicle_brands"."caption" ilike %o%)
我在做什麼錯誤的查詢?
謝謝。
請向我們展示生成的SQL。那是什麼編程語言?你使用哪個框架來生成SQL? – 2014-11-01 10:45:58