条令2-如何在多个addselect中添加限制

x7rlezfr  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(298)

问题:运行查询时出错

{
    "status": false,
    "error": {
        "classname": "Doctrine\\ORM\\Query\\QueryException",
        "message": "[Syntax Error] line 0, col 783: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_CLOSE_PARENTHESIS, got 'LIMIT'"
    }
}

如何在下面的addselect查询中添加限制。

$qb = $this->em->createQueryBuilder();
    $qb->select("identity(igsm.group) as group_id,
    qp.id AS q_paper_id,
    qp.name AS q_name,
    qp.description AS q_description,        
    qp.marks,
    identity(qp.questionPaperStatus) AS qpStatus,
    qp.timeInMinutes as time_in_minutes,
    sa.marks as marks_obtained,
    sa.id AS assessment_id");
    $qb->addSelect("(SELECT IfElse(count(qps.id) > 0 , TRUE, FALSE) FROM Entity\QuestionPaper qps "
            . "WHERE qps.createdBy = :userId AND qps.id = qp.id) as flag");
    $qb->addSelect("(SELECT count(qpe.id) from Entity\QuestionPaperEvaluation qpe "
            . "where qpe.questionPaper = qp.id AND qpe.user = :userId) as student_evaluation_cnt");
    $qb->addSelect("(SELECT LOWER(ats.assignee) from Entity\AssessmentTimelineStatus ats, Entity\AssessmentStudentTimeline ast "
            . "WHERE ast.assessmentTimelineStatus = ats.id AND ast.user = igsm.user AND ast.assessment = qp.id "
            . "ORDER BY ats.id DESC LIMIT 1) AS assignee");
    $qb->addSelect("(SELECT identity(ast.assessmentTimelineStatus) from Entity\AssessmentTimelineStatus ats, Entity\AssessmentStudentTimeline ast "
            . "WHERE ast.assessmentTimelineStatus = ats.id AND ast.user = igsm.user AND ast.assessment = qp.id "
            . "ORDER BY ats.id DESC LIMIT 1) AS assessment_timeline_id");
    $qb->addSelect("(SELECT UPPER(ConcatWs(' ', ats.action,ats.item)) as assesment_submissions from Entity\AssessmentTimelineStatus ats, "
            . "Entity\AssessmentStudentTimeline ast WHERE ast.assessmentTimelineStatus = ats.id AND ast.user = igsm.user AND ast.assessment = qp.id "
            . "ORDER BY ats.id DESC LIMIT 1) AS action");
    $qb->from('Entity\QuestionPaperGroupStudentMap', 'qpgsm');
    $qb->innerJoin('Entity\InstituteGroupStudentMap', 'igsm', 'WITH', 'igsm.id=qpgsm.instituteGroupStudentMap');
    $qb->innerJoin('Entity\QuestionPaper', 'qp', 'WITH', 'qp.id=qpgsm.questionPaper AND qp.questionPaperStatus > 4');
    $qb->leftJoin('Entity\AssesmentSubmissions', 'sa', 'WITH', 'sa.assesmentId = qp.id AND sa.userId = igsm.user');
    $qb->leftJoin('Entity\PackageQuestionPaperHistory', 'pqph', 'WITH', 'pqph.questionPaperId = qp.id');
    $qb->where('igsm.user = :userId')->setParameter(':userId', $userId);
    $qb->andWhere("igsm.group IN (:GroupIds)")->setParameter(':GroupIds', explode(",", $groupId));
    $qb->orderBy('qpgsm.id', 'DESC');
    $qb->setMaxResults(1);
    $result = $qb->getQuery()->getResult();
    return $result;

虽然子查询依赖于主查询,但我可以单独放置子查询吗。如何在子查询中添加限制。
谢谢。

hwazgwia

hwazgwia1#

在github中有一个关于这个的问题。
有人提出了同样的问题:
条令中带限制的子查询
原则2子查询限制
一个可能的快速黑客解决方案是:
在select语句中,用“ORDERBY”列作为所需select列的前缀
使用“最小值”或“最大值”聚合结果,只得到第一个结果(与“限制1”的结果相同)
此子选择:

$qb->addSelect("(SELECT LOWER(ats.assignee) from Entity\AssessmentTimelineStatus ats, Entity\AssessmentStudentTimeline ast "
        . "WHERE ast.assessmentTimelineStatus = ats.id AND ast.user = igsm.user AND ast.assessment = qp.id "
        . "ORDER BY ats.id DESC LIMIT 1) AS assignee");

将写为:

$qb->addSelect("(SELECT MAX(concat(ats.id, ' ', LOWER(ats.assignee))) from Entity\AssessmentTimelineStatus ats, Entity\AssessmentStudentTimeline ast "
        . "WHERE ast.assessmentTimelineStatus = ats.id AND ast.user = igsm.user AND ast.assessment = qp.id "
        . "ORDER BY ats.id DESC) AS assignee");

当您得到“assignee”值时,您可以拆分为第一个单独的空白值并得到“lower(ats.assignee)”的值。

$result = explode(' ',$assignee, 2);

当order by为desc时使用max,当asc时使用min。
这是因为max或min aggregate函数只选择最高值或最低值,可以是数字或字符串。
参考文献
最大/最小mysql文档
分解为第一个空格字符

相关问题