我有一个SQL查询,我想将其转换为查询构建器或Laravel ORM。它工作正常。但我希望它成为查询构建器表单或ORM。是否可以在查询构建器或ORM中编写?。
我有四个表答案,问题,用户和upvote_answers。查询有三个'连接'和一个'左连接'只是为了检查当前登录用户upvoted(boolean)答案与否以及其他属性
SELECT answers.answer_content as answer_content,
answers.id as answer_id,
answers.created_at as created_at,
answers.created_at as answer_upvote,
answers.created_at as answer_downvote,
questions.id as question_id,
questions.question_title as question_title,
questions.question_slug as question_slug,
users.id as user_id,
users.name as user_name,
users.user_slug as user_slug,
upvote_answers.upvote as upvote
FROM
answers
JOIN questions on questions.id = answers.question_id
JOIN users on users.id = answers.user_id
LEFT JOIN upvote_answers ON
upvote_answers.answer_id = answers.id AND
upvote_answers.user_id = '2'
WHERE
questions.question_active = 1 and
answers.answer_active = 1
答案 0 :(得分:1)
使用查询构建器,您可以将查询编写为
DB::table('answers as a')
->join('questions as q', 'q.id', '=', 'a.question_id')
->join('users as u', 'u.id', '=', 'a.user_id')
->leftJoin('upvote_answers as ua', function ($join) {
$join->on('ua.answer_id', '=', 'a.id')
->where('ua.user_id', '=', 2);
})
->where('q.question_active', '=', 1)
->where('a.answer_active', '=', 1)
->select(DB::raw('a.answer_content as answer_content,a.id as answer_id,a.created_at as created_at,a.created_at as answer_upvote,a.created_at as answer_downvote,q.id as question_id, q.question_title as question_title, q.question_slug as question_slug, u.id as user_id, u.name as user_name, u.user_slug as user_slug,ua.upvote as upvote'))
->get();
答案 1 :(得分:0)
使用加入条款
https://laravel.com/docs/master/queries#joins
简单示例
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->where('users.id', '=', 100)
->get();
您也可以使用Left Join Clause。
<强>更新强>
$yourQuery = \DB::table('answers')
->join('questions', 'questions.id', '=', 'answers.question_id')
->join('users', 'users.id', '=', 'answers.user_id')
->leftJoin('upvote_answers', 'upvote_answers.answer_id', '=', 'answers.id')
->where('questions.question_active', '=', '1')
->where('answers.answer_active', '=', '1')
->selectRaw('answers.answer_content as answer_content,
answers.id as answer_id,
answers.created_at as created_at,
answers.created_at as answer_upvote,
answers.created_at as answer_downvote,
questions.id as question_id,
questions.question_title as question_title,
questions.question_slug as question_slug,
users.id as user_id,
users.name as user_name,
users.user_slug as user_slug,
upvote_answers.upvote as upvote');
echo $yourQuery->toSql(); //generated sql query as string
//$yourQuery->get(); //for get data from db
结果:
选择answers.answer_content为answer_content,answers.id为 answer_id,answers.created_at as created_at,answers.created_at as answer_upvote,answers.created_at为answer_downvote,questions.id为 question_id,questions.question_title as question_title, questions.question_slug为question_slug,users.id为user_id, users.name为user_name,users.user_slug为user_slug, upvote_answers.upvote作为upvote从答案内部加入问题 questions.id = answers.question_id在users.id =上的内部加入用户 answers.user_id在upvote_answers.answer_id =上加入upvote_answers answers.id where questions.question_active =?和 answers.answer_active =?
如果你需要upvote_answers.user_id = '2'
在leftJoin必须使用提前左连接:
->leftJoin('upvote_answers', function($advancedLeftJoin){
$advancedLeftJoin->on('users.id', '=', 'contacts.user_id')
->where('upvote_answers.user_id', '=', 2);
})
最后,您的示例的答案是:
$yourQuery = \DB::table('answers')
->join('questions', 'questions.id', '=', 'answers.question_id')
->join('users', 'users.id', '=', 'answers.user_id')
->leftJoin('upvote_answers', function($advancedLeftJoin){
$advancedLeftJoin->on('users.id', '=', 'contacts.user_id')
->where('upvote_answers.user_id', '=', 2);
})
->where('questions.question_active', '=', '1')
->where('answers.answer_active', '=', '1')
->selectRaw('answers.answer_content as answer_content,
answers.id as answer_id,
answers.created_at as created_at,
answers.created_at as answer_upvote,
answers.created_at as answer_downvote,
questions.id as question_id,
questions.question_title as question_title,
questions.question_slug as question_slug,
users.id as user_id,
users.name as user_name,
users.user_slug as user_slug,
upvote_answers.upvote as upvote');
echo $yourQuery->toSql(); //generated sql query as string
//dd($yourQuery->get()); //result as collection
结果:
使用选择answers.answer_content为answer_content,answers.id为 answer_id,answers.created_at as created_at,answers.created_at as answer_upvote,answers.created_at为answer_downvote,questions.id为 question_id,questions.question_title as question_title, questions.question_slug为question_slug,users.id为user_id, users.name为user_name,users.user_slug为user_slug, upvote_answers.upvote作为upvote从答案内部加入问题 questions.id = answers.question_id在users.id =上的内部加入用户 answers.user_id在users.id =上加入upvote_answers contacts.user_id和upvote_answers.user_id =?哪里 questions.question_active =?和answers.answer_active =?
?
后, ->get()
值绑定