Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6049731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:36:35+00:00 2026-05-23T07:36:35+00:00

I have a MySQL command and I cannot find the equivalent in DQL. I

  • 0

I have a MySQL command and I cannot find the equivalent in DQL. I am trying to fetch the list most commented posts. Here is the MySQL command :

SELECT posts.id, COUNT(comments.id) AS num
FROM posts
LEFT JOIN comments ON ( posts.id = comments.post_id )
GROUP BY posts.id

Here is the result :

id  num
1   8
2   9
3   17
4   7
5   6
6   20
7   7
8   10
9   14
10  7

In DQL, it should be :

SELECT post, COUNT(comment.id) AS num
FROM Entity\Post post
LEFT JOIN post.comments comment
GROUP BY post.id

But this gives :

id  num
1   50
2   0
3   0
4   0
5   0
6   0
7   0
8   0
9   0
10  0

I don’t understand where the 50 comes from and why there is a difference between the 2 results. Could you tell me how to make this join work in Doctrine ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:36:35+00:00Added an answer on May 23, 2026 at 7:36 am

    I’ve made a few test and I found that everything seems to be fine.

    Video: id, title, ...
    Comment: id, video_id, content, ...
    

    Database schema is very simple and I think there’s no need for any explanation.

    #DQL:
        SELECT v.id, COUNT(c.id) AS num
        FROM Video v
        JOIN v.comments c
        GROUP BY v.id
        ORDER BY num DESC
    
    #Generated SQL:
        SELECT v0_.id AS id0, COUNT(v1_.id) AS sclr1 
        FROM video v0_ 
        INNER JOIN video_comment v1_ ON v0_.id = v1_.video_id 
        GROUP BY v0_.id 
        ORDER BY sclr1 DESC
    
    #Result set:
        Array
        (
            [0] => Array
                (
                    [id] => 148
                    [num] => 3
                )
    
            [1] => Array
                (
                    [id] => 96
                    [num] => 2
                )
    
            [2] => Array
                (
                    [id] => 111
                    [num] => 1
                )
    
            [3] => Array
                (
                    [id] => 139
                    [num] => 1
                )
    
        )
    

    If you select entire Video object instead of its id (v instead of v.id in SELECT clause) the query will execute as well. Of course instead of id element there will be an Video object under 0th element.

    Tested on Doctrine 2.1.0-DEV

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL innodb database at 1.9GB, showed by following command. SELECT table_schema
I have MySQL running such that I can open a client command line and
I have an extremely simple script with PHP exec , calling mysql command: $dbhost
I have a command line script that uses the Django ORM and MySQL backend.
I have MySQL installed on a Windows 2003 server on our domain. I cannot
I am trying to make a deployment war. I have the jdbc-mysql, activerecord-jdbcmysql-adapter and
I have a MySQL database I'm able to connect via the command prompt and
I am receiving the following error and cannot find the problem: You have an
I have a mysql shell but for security reasons I cannot run the mysqldump
I read about the MySQL command ON DUPLICATE KEY UPDATE . I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.