如何将mysql查询转换成yii2

时间:2018-10-22 12:27:04

标签: yii2

如果最后一个票证.p_id(子行)calendar_date为空,则选择带有calendar_date的票据。 我想在yii2搜索模型中转换此查询:

mysql查询:

`SELECT b.ticket_id FROM (SELECT a.ticket_id FROM (
SELECT t.id AS ticket_id FROM tech_support.tickets t 
WHERE t.tree_status_id = 2 AND t.p_id IS NULL) a 
LEFT JOIN tech_support.tickets tt ON tt.p_id = a.ticket_id 
WHERE tt.p_id IS NULL) b 
LEFT JOIN tech_support.tickets ttt ON ttt.id = b.ticket_id WHERE 
ttt.calendar_date IS NULL UNION ALL
SELECT b.maxid FROM (SELECT a.maxid, tt.calendar_date FROM (
SELECT MAX(t.id) AS maxid FROM tech_support.tickets t WHERE 
t.tree_status_id = 2  GROUP BY t.p_id) a 
LEFT JOIN tech_support.tickets tt ON tt.id = a.maxid) b 
WHERE b.calendar_date IS NULL;`

yii2代码:

$subQuery = Tickets::find()
            ->select(new Expression('id as ticket_id'))
            ->where('tree_status_id = 2')
            ->andWhere('p_id is null')
            ->alias('a');
$subQuery->leftJoin('tickets', 'tickets.p_id = a.ticket_id')
            ->where('tickets.p_id is null')->all();
$query1 = (new \yii\db\Query())
            ->select(new Expression('id as ticket_id'))
            ->from($subQuery)
            ->where('p_id is null')
            ->alias('b');
$query1->leftJoin(['ttt' => 'tickets'], 'ttt.id = 
        b.ticket_id')
            ->where('ttt.calendar_date IS NULL');
$subQuery2 = Tickets::find()
            ->select(new Expression('MAX(tickets.id) as maxid'))
            ->where('tree_status_id = 2')
            ->groupBy(['p_id'])
            ->alias('a');
$subQuery2->leftJoin(['tt' => 'tickets'], 'tt.id = a.maxid')
            ->all();
$query2 = (new \yii\db\Query())
            ->select('maxid')
            ->from($subQuery2);

$query1->union($query2);
$query1->where('calendar_date is null');

错误信息: 违反完整性约束– yii \ db \ IntegrityException
请帮助我。

1 个答案:

答案 0 :(得分:0)

结果:

//----------------query1------------------------------
        $query = (new \yii\db\Query())
            ->select('t.id as ticket_id')
            ->from(['t' => 'tickets'])
            ->where('t.tree_status_id = 2')
            ->andWhere('t.p_id is null');
        $subQuery = (new \yii\db\Query())
            ->select('a.ticket_id')
            ->from(['a' => $query]);
        $subQuery->leftJoin(['tt' => 'tickets'], 'tt.p_id = a.ticket_id')
            ->where('tt.p_id is null');
        $query1 = (new \yii\db\Query())
            ->select('b.ticket_id')
            ->from(['b' => $subQuery]);
        $query1->leftJoin(['ttt' => 'tickets'], 'ttt.id = b.ticket_id')
            ->where('ttt.calendar_date IS NULL');
        //----------------query2------------------------------
        $subQuery2 = (new \yii\db\Query())
            ->select('MAX(t.id) as maxid')
            ->from(['t' => 'tickets'])
            ->where('t.tree_status_id = 2')
            ->groupBy(['t.p_id']);
        $query2 = (new \yii\db\Query())
            ->select(['a.maxid', 'tt.calendar_date'])
            ->from(['a' => $subQuery2]);
        $query2->leftJoin(['tt' => 'tickets'], 'tt.id = a.maxid');
        $query3 = (new \yii\db\Query())
            ->select('b.maxid')
            ->from(['b' => $query2]);
        $query3->leftJoin(['ttt' => 'tickets'], 'ttt.id = b.maxid')
            ->where('ttt.calendar_date IS NULL');
        //----------------union queries------------------------------
        $unionQuery = (new \yii\db\Query())
            ->from([$query1->union($query3, true)]);