2012-01-17 77 views
2

学说更新查询我愿做一个更新询问有那样的极限:与LIMIT

UPDATE anytable SET anycolumn = 'anyvalue' WHERE anothercolumn='anothervalue' LIMIT 20 

这怎么可能教义2.1?

回答

2

不是学说特定的,但也许可以用子查询?

UPDATE messages SET test_read=1 
WHERE id IN (
    SELECT id FROM (
     SELECT id FROM messages 
     ORDER BY date_added DESC 
     LIMIT 5, 5 
    ) tmp 
); 
+0

'选择':错误:类 '(' 不defined' – 2013-07-05 13:15:37

-2

编辑:

,你可以去一下两种不同的方式:

1 - 创建查询直接使用DQL:

$query = $entityManager->createQuery('UPDATE Entities\User u SET u.someValue = newValue WHERE u.id = someId'); 

// this will add the LIMIT statement 
$query->setMaxResults(20); 

$query->execute(); 

2 - 创建使用QueryBuilder进行查询:

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

$query = $qb->update('Entities\User', 'u') 
      ->set('u.someValue', newValue) 
      ->where('u.id = someId') 
      ->getQuery(); 

// this will add the LIMIT statement 
$query->setMaxResults(20); 

$query->execute(); 

你应该做的:回声$query->getSQL();检查出这两个

编辑生成的SQL: 另一种选择(不强烈推荐)是使用Native SQL

+0

不,它不需要帮助。我需要一个更新多个表的查询,但不是所有的表。所有这一切在一个查询中。我不想选择一些,然后逐个更新。 – scube 2012-01-17 16:28:47

+0

好的。我会在一秒内编辑 – jere 2012-01-17 16:39:42

+0

@scube看看这是否接近你所需要的 – jere 2012-01-17 18:16:34

4

我发现我有从entityManager获取连接并调用executeUpdate:

$em->getConnection()->executeUpdate(
    "UPDATE anytable SET anycolumn = 'anyvalue' 
    WHERE anothercolumn='anothervalue' 
    LIMIT 20"); 

该说:

If you want to execute DELETE, UPDATE or INSERT statements the Native SQL API cannot be used and will probably throw errors. Use EntityManager#getConnection() to access the native database connection and call the executeUpdate() method for these queries.