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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:19:47+00:00 2026-05-13T11:19:47+00:00

I have 2 tables. One table contains posts and the other contains votes for

  • 0

I have 2 tables. One table contains posts and the other contains votes for the posts. Each member can vote (+ or -) for each post.
(Structure example:)

  • Posts table: pid, belongs, userp, text.
  • Votes table: vid, userv, postid, vote.

Also one table which contains the info for the users.

What I want is: Supposing I am a logged-in member. I want to show all the posts, and at those I’ve already voted, not let me vote again. (and show me what I have voted + or -)

What I have done til now is very bad as it will do a lot of queries:

SELECT `posts`.*, `users`.`username` 
FROM `posts`,`users` 
WHERE `posts`.belongs=$taken_from_url AND `users`.`usernumber`=`posts`.`userp` 
ORDER BY `posts`.`pid` DESC;

and then:

foreach ($query as $result) {if (logged_in) {select vote from votes....etc} }

So, this means that if I am logged in and it shows 30 posts, then it will do 30 queries to check if at each post I have voted and what I’ve voted. My question is, can I do it shorter with a JOIN (I guess) and how? (I already tried something, but didn’t succeed)

  • 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-13T11:19:48+00:00Added an answer on May 13, 2026 at 11:19 am

    Firstly I’ll say that if you’re going to have significantly different output for users logged in versus those that aren’t, just have two queries rather than trying to create something really complicated.

    Secondly, this should do something like what you want:

    SELECT p.*, u.username,
      (SELECT SUM(vote) FROM votes WHERE postid = p.pid) total_votes,
      (SELECT vote FROM votes WHERE postid = p.pid AND userv = $logged_in_user_id) my_vote
    FROM posts p
    JOIN users u ON p.userp = u.usernumber
    WHERE p.belongs = $taken_from_url
    ORDER BY p.pid DESC
    

    Note: You don’t say what the values of the votes table are. I’m assuming it’s either +1 (up) or -1 (down) so you can easily find the total votes by adding them up. If you’re not doing it this way I suggest you do to make your life easier.

    The first correlated subquery can be eliminated by doing a JOIN and GROUP BY but I tend to find the above form much more readable.

    So what this does is it joins users to posts, much like you were doing except that it uses JOIN syntax (which again comes down to readability). Then it has two subqueries: the first finds the total votes for that particular post and the second finds out what a particular user’s vote was:

    • +1: up vote;
    • -1: down vote;
    • NULL: no vote.
    • 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 table contains comments on posts, another contains commenter information
I have a one to many relationship between two tables. The many table contains
Here is my situation: I have one table that contains a list of drugs
I have one database table which contains 8 columns. One of the columns is
I have a TABLE elements with one COLUMN number, type SMALLINT that contains numbers
I have two tables, one that contains volunteers, and one that contains venues. Volunteers
I have two tables, one contains the page/heading number and the heading text, the
I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I have two tables, one for routes and one for airports. Routes contains just
i have a large mysql database table in which one column contains values ranging

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.