2017-06-16 127 views

回答

0

使用raw query,如:使用DB::satement

$query='SELECT u.*,(SELECT COUNT(*) FROM (
SELECT id,author_id FROM posts p 
UNION ALL 
SELECT id, author_id FROM updates up) total_table WHERE author_id = u.id) as total_post FROM users u ORDER BY total_post'; 
$data=DB::statement($query); 

像這樣

DB::select(DB::raw('SELECT u.*,(
SELECT COUNT(*) FROM (
    SELECT id,author_id FROM posts p 
    UNION ALL 
    SELECT id, author_id FROM updates up 
) total_table WHERE author_id = u.id) as total_post FROM users u ORDER BY total_post')); 

Reference

0

或者你也可以做到這一點。