在同一个表中使用子查询删除原则

时间:2018-06-18 08:57:54

标签: mysql symfony doctrine

我正在尝试使用doctrine

从mysql中删除记录

对应的MYSQL查询

DELETE FROM user_fields_data WHERE id NOT IN (
    SELECT * FROM (
        SELECT id FROM user_fields_data WHERE id IN (
            SELECT block_id FROM user_fields_data WHERE template = 1
        )
    ) AS `ss`
) AND template = 1

尝试使用Doctrine删除:

$qb = $this->_em->createQueryBuilder();

$blockId = $qb->select('bl.blockId')
    ->from('UserFieldBundle:UserFieldsData','bl')
    ->where('bl.template = 1');

$ids = $qb->select('bl2.id')
    ->from('UserFieldBundle:UserFieldsData','bl2')
    ->where($qb->expr()->in('bl2.id',$blockId->getDQL()));

$result = $qb->delete('UserFieldBundle:UserFieldsData','USF')
    ->where($qb->expr()->notin('USF.id', $ids->getDQL()))
    ->andWhere('USF.template = :templateId')
    ->setParameter('templateId', 1)
    ->getQuery()
    ->getSQL();

dump($result);

Doctrine查询以错误结束:

  

[语法错误]第0行,第65行:错误:预期文字,得到'删除'“

数据库结构:

https://www.db-fiddle.com/f/8V8ctsecVBKSNgsCrhuc4/1

0 个答案:

没有答案