Understanding Bulk UPDATE Statements Using Sub-Contact in Doctrine

Asked 2 years ago, Updated 2 years ago, 83 views

This is for the bulk UPDATE statement in Doctorline 2.
Is it possible to rewrite the following SQL in DQL?

In MySQL, you may want to keep running SQL running, such as the following:

$this->getConnection()->query(sprintf(
US>'UPDATE%sp,(SELECT c.post_id, count(*) as total FROM%sasc WHERE c.status=1GROUP BY c.post_id)as count_comment
SET p.comments_count = count_comment.total
WHERE p.id = count_comment.post_id'
, $postTableName, $commentTableName);

I wanted to use PostgreSQL, so I thought about rewriting it to DQL, but it doesn't work well.

It seems that the DQL documentation says that you can use sub-selects.

References to related entries are only possible in the WHERE claim and using > sub-selects.

php symfony2 doctrine2

2022-09-29 20:29

1 Answers

References to related entries are only possible in the WHERE claim and using sub-selects.

When I read EBNF, the values specified in the SET clause, as well as the FROM clause, are also SimpleArthmaticExpressions that do not allow subqueries.The English sentence above probably means that you can only refer to the associated entity when you use the subquery in the WHERE clause.

If that's the case, I can't rewrite SQL using subqueries in DQL, so why don't you use SQL?

php-Doctorline Update query with a sub-query-Stack Overflow

* Below is the answer I wrote when https://ja.stackoverflow.com/revisions/7586/9

Sub-select is probably like this."Personally, I think this is ""sub-question""…

"
UPDATE hoge
SET foo=(SELECT count(*) FROM piyo)

and so on

UPDATE hoge
SET foo=1
WHERE bar IN (SELECTED FROM piyo)

MySQL documentation says Subquery.

MySQL::MySQL 5.7 Reference Manual::13.2.10 Subquery Syntax

UPDATE table1,table2SET... is a proprietary extension of MySQL and cannot be used in DQL, which is a comment written in JIRA and StackOverflow cited in the question.

I don't know about QueryBuilder, but how about rewriting to SQL using a sub-question?This SQL probably works with MySQL or PostgreSQL.

UPDATE %sp 
SET
  p.comments_count =(
    SELECT count(*) FROM %sc WHERE c.status=1AND c.post_id=p.id
  )


2022-09-29 20:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.