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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:45:25+00:00 2026-05-20T12:45:25+00:00

I have two tables, one for articles/polls, and one for the votes on these

  • 0

I have two tables, one for articles/polls, and one for the votes on these articles. Votes are held in an enum field for up/down, and a couple other options. I am trying to create a query that will bring back the data I need for the article, and also give me the sum of all the votes. Unfortunately, I am getting an unexpected result. It is tallying all my votes as though they were cast for the first entry, and none for any others.

How can I properly link my votes to the polls they belong with?

tblVotes
[PK] primaryId  |    voterId (FK)  | voteValue (enum) |    postID (fk)
----------------------------------------------------------------------
     1                    10                 up              1
     2                    11                 down            1
     3                    11                 up              10

tblContent
[PK] unique Id |   postTitle   |   postBody  |   postAuthor(FK)  |
------------------------------------------------------------------
       1               foo           foofoo          12
      10               bar           barbar          10
      11               foobar    foofoobarbar        10

When I tun the query detailed below, I expect I would get results like this:

uniqueId |userName | pollDate | postTitle  |postBody  | upVotes| downVotes
--------------------------------------------------------------------------
   1         bob      1/1/11      foo        foofoo      1         1
  10         john     1/2/11      bar        barbar      1         null
  11         john     1/3/11      foofoo     foofoobar.. null      null

Unforunately, I am actually getting results like this:

uniqueId |userName | pollDate | postTitle  |postBody  | upVotes| downVotes
--------------------------------------------------------------------------
   1         bob      1/1/11      foo        foofoo      2         1
  10         john     1/2/11      bar        barbar      null      null
  11         john     1/3/11      foofoo     foofoobar.. null      null

Here is my SQL statement:

            SELECT
                tblContent.uniqueID,
                tblUsers.userName,
                tblContent.postDate,
                tblContent.postTitle,
                tblContent.postBody,
                votes.upVotes,
                votes.downVotes
            FROM
                tblContent
                    LEFT JOIN
                    (
                        SELECT
                            postId,
                            SUM(CASE WHEN tblVotes.voteType = 'up' THEN 1 ELSE 0 END) as upVotes,
                            SUM(CASE WHEN tblVotes.voteType = 'down' THEN 1 ELSE 0 END) as downVotes
                        FROM
                            tblVotes, tblContent
                        WHERE
                            tblContent.uniqueId = tblVotes.postId
                     ) votes
                    ON tblContent.uniqueId = votes.postId
                    LEFT JOIN
                        tblUsers
                    ON tblUsers.userId = tblContent.postAuthor
            WHERE
                postApproved = true
            ORDER BY
                postDate DESC
  • 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-20T12:45:26+00:00Added an answer on May 20, 2026 at 12:45 pm

    Needed to GROUP BY in the nested SQL statement.

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

Sidebar

Related Questions

I have two tables. One is article and the other structure. And the articles
I have two tables : one table it matchs and the other is teams.
Using PHP / MySQL I have two tables one is customers the other is
I have two tables. One is Employee_Mstr and other is EmployeeLeaveRequest_mstr . My data:
I have two tables. One contains members and the other members dogs . The
I have two tables in MySql database, one is sales_order and the other is
I have two tables. One of events and one of articles. Each event has
I have two tables with these structure: articles: article_id, text tags: tag_id, article_id, text
I've got two tables (articles and tags) that have a one-to-many relationship. I remember
I have two mysql tables, one listing article names, the other listing the authors

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.