如何将查询语句转换为DB :: raw eloquent laravel?

时间:2017-06-16 10:36:07

标签: php mysql database laravel-5

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

我有查询行想要DB :: raw。你能帮助我吗???非常感谢

2 个答案:

答案 0 :(得分:0)

使用raw 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

答案 1 :(得分:0)

或者您可以使用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);

喜欢这个。