我是laravel的新手。这是我的查询,过滤掉没有任何问题的问题(问题)或问题没有任何答案。但是我不确定如何使用laravel查询构建器正确编写它。考虑到我使用雄辩来建立关系如果我能够使用laravel eloquent得到相同的结果会更好。
SELECT DISTINCT q.id, q.content
FROM m_quiz q
RIGHT JOIN (SELECT DISTINCT p.quiz_id as id
FROM m_problem p
RIGHT JOIN m_answer_choice a
ON p.id = a.problem_id) problem
ON q.id = problem.id
ORDER BY q.id;
答案 0 :(得分:0)
未经测试,但我认为会是这样的。
DB::table('problems')->selectRaw('m_problem.*, DISTINCT m_problem.quiz_id as id')
->join('m_answer_choice', 'm_problem.id', '=', 'm_answer_choice.problem_id')
->groupBy('m_problem.id')
->get();
但如果你可以做一些像
这样的事情会更好namespace App;
use Illuminate\Database\Eloquent\Model;
class Problem extends Model
{
/**
* Get the comments for the blog post.
*/
public function answerChoices()
{
return $this->hasMany('App\AnswerChoices');
}
}
并执行类似
的操作$answerChoices= App\Problem::find(1)->answerChoices;
foreach ($answerChoices as $answerChoice) {
//
}
答案 1 :(得分:0)
SELECT
*, SUM(num_hr) AS total_hr, attendances.empID AS empid
FROM attendances
LEFT JOIN addsalaryemployees ON addsalaryemployees.empID=attendances.empID
LEFT JOIN positions ON positions.id=addsalaryemployees.empPosition
GROUP BY attendances.empID
ORDER BY addsalaryemployees.empFirstName ASC