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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:45:40+00:00 2026-06-11T20:45:40+00:00

I have two databases: one contains the users: persons id name course the other

  • 0

I have two databases: one contains the users:

persons

id
name
course

the other one contains the

voters

   id
   personID
   type

An entry to the voters table gets added when the user voted (type describes different topics a user can vote on)

I want to get all users for example where persons.course = '1' AND voters.type = '2' and have the output contain the following columns: id, name, hasVoted (where hasVoted returns true if the personID can be found in the voters table)

  • 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-11T20:45:41+00:00Added an answer on June 11, 2026 at 8:45 pm

    A left join may be what your looking for.

    SELECT persons.id, persons.name, voters.personID IS NOT NULL AS hasVoted
    FROM persons LEFT JOIN voters ON (persons.id = voters.personID)
    WHERE persons.course = '1' AND voters.type = '2'
    GROUP BY persons.id
    

    http://dev.mysql.com/doc/refman/5.0/en/join.html

    The left join will include all the matching rows from the left side of the join, and any matching pieces from the right side of the join. Any rows from the left side without a matching right side will be have the right side columns values of null.

    As for the ‘IS NOT NULL’ or ‘IS NULL’ is used because ‘X = NULL’ doesn’t behave as it appears.

    > SELECT 'A' = NULL, 'A' IS NULL, NULL = NULL, NULL IS NULL;
    +------------+-------------+-------------+--------------+
    | 'A' = NULL | 'A' IS NULL | NULL = NULL | NULL IS NULL |
    +------------+-------------+-------------+--------------+
    |       NULL |           0 |        NULL |            1 |
    +------------+-------------+-------------+--------------+
    

    More details about working with nulls:
    http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html

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

Sidebar

Related Questions

What I have is two tables inside of a mysql database. One table contains
I have a table and two databases which have the same table, but one
I have two data tables. One contains all of users identifiable informaiton (username, id,
say i have two collections in mongodb,one for users which contains users basic info,and
I have a database with two tables. One of the tables contains users, the
I have two Xapian databases, let's call one EN and the other DE, and
My database has two tables, one contains a list of users, the other a
I have a database which has two tables. One table contains the information of
I have two databases on one machine that also has two different sites running
I have two databases in SQL2k5: one that holds a large amount of static

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.