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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:22:01+00:00 2026-05-22T17:22:01+00:00

I need to know how to link two tables in a php/mysql set up

  • 0

I need to know how to link two tables in a php/mysql set up then rank the results?
Here is my situation.

I have a stories table:

storyid
writerid
title
story
submitdate

and a votes table

voteid
userid
storyid
vote

I store a vote up as a 1 and a vote down as a -1
I am looking for a way to join these two table then rank/sort the stories by the number of votes they recieve.
I am open to any ideas about how to do so or a different possible database schema.

  • 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-22T17:22:02+00:00Added an answer on May 22, 2026 at 5:22 pm

    I prefer to keep the names of my tables singular. It’s not a “Stories” table; it’s a “Story” table with multiple rows.

    A vote can only be attributed to a single story, so it’s a one-to-many relationship between the two. I’d put the foreign key in the votes table and let it point out the story it’s associated with. Change your schema if you agree: remove the voteid from the story table and make storyid in vote a foreign key to the story table.

    But with that said, perhaps you can try a query like this:

    select stories.storyid, sum(vote=-1) as down, sum(vote=1) as up
    from stories
    inner join votes on (stories.storyid = votes.storyid)
    group by stories.storyid
    

    Corrected per ypercube’s comment below.

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

Sidebar

Related Questions

I have a MySql db with innoDB tables. Very simplified this is how two
I have two buttons, and need to link them with a line. I bind
I have two tables called TableA and TableB. TableA has the following fields: TableA_ID
Example: I have two tables in my database called classA and classB , and
I have an assignment where I am to create two tables within a database.
Here's an example: Originally I have 3 tables. Table B references Table A. So
As I understand it, once I've set up an RMI communications link between two
I have two tables: A table of restaurants and a table of areas. The
Need help badly guys, I have three tables tbl_questions, tbl_answers and tbl_link_qa tbl_questions is
Hey guys need some more help I have 3 tables USERS, PROFILEINTERESTS and INTERESTS

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.