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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:31:44+00:00 2026-06-02T07:31:44+00:00

*The actual problem is quite more complex than the post title :S Suppose I

  • 0

*The actual problem is quite more complex than the post title :S

Suppose I have table

ID | vote_id |  vote_type |  vote_user

1  ----  2  -----   up -------    Tom
2  ----  3  -----   up -------    John
3  ----  3  -----   down -----    Harry
4  ----  4  -----   up -------    Tom
5  ----  2  -----   down -----    John
6  ----  3  -----   down -----    Tom

So what I want to do is

  1. Query the table by specific vote_id
  2. Then Count how many voted up and how many voted down from the query in 1
  3. Then I want to check also that had John voted for that vote_id or not.

And the way I do is.

$up=0;
$down=0;
$voted="no"; 
$r=mysql_query("SELECT*FROM table_name WHERE vote_id == 3");

 while($row=mysql_fetch_array($r){
   if($row["vote_type"]=="up"){ $up++;}else{$down++;}
   if($row["vote_user"=="John"){ $voted="yes";}
 }

But is there a way ( equivalent code ) to do this without using WHILE LOOP because the actual table can be very large so running while loop can be very exhaustive :S

EDIT Can I do with single query?

  • 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-02T07:31:45+00:00Added an answer on June 2, 2026 at 7:31 am

    In a single query

    Select
      sum(case when vote_type = 'up' then 1 else 0 end) as up,
      sum(case when vote_type = 'down' then 1 else 0 end) as down,
      sum(case when vote_user = 'john' then 1 else 0 end) as john
    from
      yourTable
    where
      vote_id = 3
    

    Or any variation of sum(case when)

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

Sidebar

Related Questions

This is more of a theoretical question than an actual problem I have. If
This is something now more of curiosity than actual purpose. If you have a
I have the following code: The actual problem is the non-quoted code. I want
Am facing a problem, while creating components through TOM API using .NET/COM Interop. Actual
The problem is quite simple, I have a method that returns a list. I
I am very new to sql.The actual problem is much bigger. I need information
i know that it may be difficult to see the actual problem without looking
Ok, my actual problem was this: I was implementing an IList<T> . When I
I've created an example of the problem here: http://jsfiddle.net/JustinN/qWeLT/1/ My actual code is hooked
I am starting to learn Java. The actual language is not a problem since

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.