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

  • Home
  • SEARCH
  • 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 8892437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:59:49+00:00 2026-06-14T22:59:49+00:00

Background info System OS: bash$ lsb_release -a No LSB modules are available. Distributor ID:

  • 0

Background info

System

OS:

bash$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 6.0.5 (squeeze)
Release:        6.0.5
Codename:       squeeze

MySQL:

bash$ mysql --version
mysql  Ver 14.14 Distrib 5.1.63, for debian-linux-gnu (x86_64) using readline 6.1

Database

Engine: MyISAM

Table: post_votes

mysql> describe post_votes;
+-----------+---------------------+------+-----+---------+----------------+
| Field     | Type                | Null | Key | Default | Extra          |
+-----------+---------------------+------+-----+---------+----------------+
| id        | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| post_uuid | binary(16)          | YES  |     | NULL    |                |
| user_uuid | binary(16)          | YES  |     | NULL    |                |
| vote      | tinyint(4)          | YES  |     | 0       |                |
+-----------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

Table: posts

mysql> describe posts;
+-------------+---------------------+------+-----+----------------------+----------------+
| Field       | Type                | Null | Key | Default              | Extra          |
+-------------+---------------------+------+-----+----------------------+----------------+
| id          | bigint(20) unsigned | NO   | PRI | NULL                 | auto_increment |
| post_uuid   | binary(16)          | YES  | UNI | NULL                 |                |
| owner_uuid  | binary(16)          | YES  |     | NULL                 |                |
| created     | int(10) unsigned    | YES  |     | NULL                 |                |
| edited      | int(10) unsigned    | YES  |     | NULL                 |                |
| title       | varchar(50)         | YES  |     | NULL                 |                |
| description | varchar(150)        | YES  |     | NULL                 |                |
| link        | varchar(100)        | YES  |     | NULL                 |                |
| properties  | varchar(20)         | YES  |     | 00000000000000000000 |                |
+-------------+---------------------+------+-----+----------------------+----------------+
9 rows in set (0.00 sec)

The Problem

I want to select posts depending on their upvote/downvote ratio. I’ve come up with a query that counts upvotes and downvotes but can’t seem to join it to the actual posts.

The working (uncomplete) query

(
    SELECT COUNT(*), post_uuid, 'upvotes' AS 'mode'
    FROM post_votes a
    WHERE a.vote = 1
    GROUP BY post_uuid
)
UNION
(
    SELECT COUNT(*), post_uuid, 'downvotes' AS 'mode'
    FROM post_votes a
    WHERE a.vote = -1
    GROUP BY post_uuid
)

The broken query

(
    (
        SELECT COUNT(*), post_uuid, 'upvotes' AS 'mode'
        FROM post_votes a
        WHERE a.vote = 1
        GROUP BY post_uuid
    )
    UNION
    (
        SELECT COUNT(*), post_uuid, 'downvotes' AS 'mode'
        FROM post_votes a
        WHERE a.vote = -1
        GROUP BY post_uuid
    )
)
a
LEFT JOIN posts b ON a.post_uuid = b.post_uuid

What am I doing wrong? the error message I get is:

ERROR 1064 (42000): 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 'UNION
(
SELECT COUNT(*), post_uuid, 'downvotes' AS 'mode'
  • 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-06-14T22:59:51+00:00Added an answer on June 14, 2026 at 10:59 pm

    The UNION pair is effectively a derived table and must appear in a FROM clause to participate in the JOIN. Since it is in the FROM clause now, you must SELECT its columns, otherwise your query has no SELECT clause at all. MySQL’s syntax is lenient, but not so lenient as to permit a missing SELECT clause.

    /* SELECT the cols produced by the UNION */
    SELECT 
       a.`num`, 
       a.`post_uuid`,
       a.`mode`,
      /* And other columns from `posts` if needed */
       b.`something`,
       b.`something_else`,
       b.`some_other_thing`
    /* UNIONs are a derived table, belonging in FROM */
    FROM (
        (
            SELECT COUNT(*) as num, post_uuid, 'upvotes' AS 'mode'
            FROM post_votes a
            WHERE a.vote = 1
            GROUP BY post_uuid
        )
        UNION
        (
            SELECT COUNT(*) num, post_uuid, 'downvotes' AS 'mode'
            FROM post_votes a
            WHERE a.vote = 1
            GROUP BY post_uuid
        )
    ) a
    LEFT JOIN posts b ON a.post_uuid = b.post_uuid
    

    Looking over this, I believe you can simplify away the UNION by using a SUM(CASE) aggregate pattern:

    SELECT 
      b.post_uuid,
      SUM(CASE WHEN a.vote = 1 THEN 1 ELSE 0 END) AS upvotes,
      /* Assume -1 for downvotes? */
      SUM(CASE WHEN a.vote = -1 THEN 1 ELSE 0 END) AS downvotes
    FROM
      posts b
      LEFT JOIN post_votes a ON a.post_uuid = b.post_uuid
    GROUP BY b.post_uuid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some background info I am programming in a system that uses a proprietary programming
I am making a website with a voting system, for some background info, this
Background info: I'm using Microsoft Visual C# 2010 for this project. I want to
Background info I'm developing a visual form designer in which people with no coding
Background info : I was handed a Tool, which was made using MS-Access 2007,
Background info: I have a windows application (in c#) that handles a custom file
Background info: I've recently decided to take on a project of making a social
Background info I recently handed in an assigment for my class on algorithms and
Some background info: I am currently writing a bootloader in protected mode while learning
Some background info: I'm trying to run a server program in python 2.5.1 (the

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.