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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:56:11+00:00 2026-06-18T03:56:11+00:00

I have 5 MySQL data fields for a votes table: post id poll id

  • 0

I have 5 MySQL data fields for a votes table:

  • post id
  • poll id
  • vote id
  • voter
  • voteid

You can vote in a poll or vote for a post. If you vote in a poll, the post/person field will be null. If you vote for a post, the vote field will be null.

I want to set up the table so it will allow you to make either the post id or vote id null, but not both. I’m using phpmyadmin to manage my database.

Any ideas?

  • 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-18T03:56:12+00:00Added an answer on June 18, 2026 at 3:56 am

    I have to agree with jmilloy above the best thing to do is to create separate tables. This an example how this would work:

    Table structure and sample data:

    CREATE TABLE post (
        post_id INT AUTO_INCREMENT PRIMARY KEY,
        vote_id INT
     );
    
    CREATE TABLE poll (
        poll_id INT AUTO_INCREMENT PRIMARY KEY,
        vote_id INT
      );
    
    CREATE TABLE voter(
      vote_id INT AUTO_INCREMENT PRIMARY KEY,
      name VARCHAR(30)
      );
    
    INSERT INTO post (vote_id) VALUES(1),(2),(3),(6);
    
    INSERT INTO poll (vote_id) VALUES(3),(5),(4),(7);
    
    INSERT INTO voter(name) VALUES ('bob'),
    ('Jack'),
    ('Joe'),
    ('Shara'), 
    ('Hillary'), 
    ('Steven'), 
    ('Sandra');
    

    To retrieve the voter that has voted for a post you have to use a JOIN. This is an example how this would look like if you want to find the voters for a post number 2.

    SELECT post.post_id, vote.name  
    FROM (post 
     JOIN post_vote 
    ON post_vote.post_id = post.post_id) 
     JOIN vote 
    ON vote.vote_id = post_vote.vote_id
    WHERE post.post_id = 2;
    

    SQL FIDDLE DEMO

    Some explanation if you have a poll and a vote you have a many to many relationship, i.e. one voter can vote for more than one poll and one poll can have more than one voter. To bridge between the vote and poll table you use a bridge table. This tables contains all the poll numbers and vote combinations. So when you want to know who has voted for a particular poll you need to link the poll_id with the poll_id in the poll_vote table. The result is then matched with the vote table using the vote_id in the poll_vote table and the vote table. Hope this helps. Good luck with your project.

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

Sidebar

Related Questions

I have a MySQL table with a tree data structure. The fields are _id
I have a mysql table field set as time type which stores data in
I have a mysql table with 3 fields: id, client, group and some data.
I have a mysql table and one of the fields is 'template'. This field
I have set my MySQL field table data type to the decimal because from
I have a MySQL table with next fields: id int(11) primary auto_increment data(text) user
I have a MySQL table with fields and data such as follows; PartNumber Priority
I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
I have two MySQL tables describing data that can be extended into subclasses, one
I have a mysql table with over 4 million of data; well the problem

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.