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 7605983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:18:01+00:00 2026-05-31T00:18:01+00:00

I have 2 tables that i have to get last 10 active project ids

  • 0

I have 2 tables that i have to get last 10 active project ids from based on the date. The query is:

(
SELECT project_posts.projectid as projectId, `project_posts`.`date` as rowDate
FROM `project_posts` ORDER BY `project_posts`.id DESC
)
UNION ALL
(SELECT `project_comments`.projectid as projectId, project_comments.`date` as rowDate FROM `project_comments` ORDER BY `project_comments`.id DESC) GROUP BY projectId ORDER BY rowDate DESC LIMIT 10

And i want to sort it by rowDate DESC (last project first). The problem is that when i add group by MySQL says:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'GROUP BY projectId ORDER BY rowDate DESC LIMIT 6' at line 9

I looked over the internet for problems with union, union all and group by, but I didn’t find any solution.
Thanks in advance.

  • 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-31T00:18:02+00:00Added an answer on May 31, 2026 at 12:18 am

    I think you need to create a derived table from your union query before you can group on it –

    SELECT projectId, MAX(rowDate) AS rowDate
    FROM (
        (
            SELECT project_posts.projectid as projectId, `project_posts`.`date` as rowDate 
            FROM `project_posts`
        ) UNION ALL (
            SELECT `project_comments`.projectid as projectId, project_comments.`date` as rowDate
            FROM `project_comments`
        )
    ) AS tmp
    GROUP BY projectId
    ORDER BY rowDate DESC
    LIMIT 10
    

    I have not had a chance to try this but it should do what you are after –

    SELECT p.id, GREATEST(IFNULL(MAX(pc.date), 0), IFNULL(MAX(pp.date), 0)) AS max_date
    FROM projects p
    LEFT JOIN project_comments pc
        ON p.id = pc.projectid
    LEFT JOIN project_posts pp
        ON p.id = pp.projectid
    GROUP BY p.id
    ORDER BY max_date DESC
    LIMIT 10
    

    UPDATE The UNION based query should be significantly faster than this second query. I have done some crude tests using some dummy data (100k projects, 200k posts and 700k comments) and the multi join query takes three to five times longer than the UNION query.

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

Sidebar

Related Questions

I have 4 Tables (listed bellow) and need: get last 10 Chats from Room
I'm using Castle ActiveRecord in a warehouse project. I have several tables that get
I have a 3 tables that I'm trying to join and get distinct results.
I have a large table that get populated from a view. This is done
I have a main table that I must get data from. I have a
I have some tables that benefit from many-to-many tables. For example the team table.
I have several tables that get JOINed together to form a table with columns
I need a query that will have the date, a user's name, and one
Ok, I have two tables that I am joining and doing a select on
I have a database with many tables that get used, and many tables that

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.