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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:21:54+00:00 2026-06-14T22:21:54+00:00

So am trying to write a MySQL query with a simple join to pull

  • 0

So am trying to write a MySQL query with a simple join to pull things from two different tables.

The fist table is a students table (Sorry its not prettier I couldn’t find a better way to put them in)

id  first_name  last_name   major    phone_number
-------------------------------------------------
1   Eric        Larsen      CS       1234567891
2   Sam         Coons       English  1234567891
3   David       Brown       MAE      1234567891
4   Richard     Brown       CS       1234567891
5   Kendra      Griffiths   FCHD     1234567891

The second is a scores table, rich now it just has one row,

id  student_id  item_id  score
------------------------------
1   1           1        20

There is a third table with a list of items but is not really needed for this particular query.

I want to pull all of the students along with there score for scores.item_id = 1 if they have a score, if they don’t I still want it to pull the information for the student, well, there name at least.

So here is the query I wrote.

SELECT students.first_name, students.last_name, scores.score
FROM students
     LEFT JOIN scores
     ON students.id = scores.student_id
WHERE scores.item_id =1

In my mind this should work, it should pull all the students even if there is no score for them because it is a left join but it is only pulling the one student with a score Eric Larsen, The results looks like

Eric, Larsen, 20

But shouldn’t it be:

first_name  last_name  score
----------------------------
Eric        Larsen     20
Sam         Coons      NULL
David       Brown      NULL
Richard     Brown      NULL
Kendra      Griffiths  NULL

?

  • 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-14T22:21:55+00:00Added an answer on June 14, 2026 at 10:21 pm

    This query will reveal your problem.

    SELECT students.first_name, students.last_name, scores.score, scores.item_id
        FROM students
        LEFT JOIN scores
            ON students.id = scores.student_id
    

    You see scores.item_id is NULL for all those other rows, so that fails your WHERE scores.item_id = 1 clause, hence those rows not appearing in your results.

    This should get the results you are after

    SELECT students.first_name, students.last_name, scores.score
        FROM students
        LEFT JOIN scores
            ON (students.id = scores.student_id)
        WHERE (scores.item_id = 1 OR scores.item_id IS NULL)
    

    or

    SELECT students.first_name, students.last_name, scores.score
        FROM students
        LEFT JOIN scores
            ON (students.id = scores.student_id AND scores.item_id = 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make simple mysql select query, I have 3 tables post:
I am trying to write a Bash script to run a MySQL query from
Simple mysql question i am trying to write a query based on a criteria
I am trying to write a simple stream, streaming everything from a mysql field.
I'm trying to write a MySQL query (from PHP) that will find the newest
I am trying to write a MySQL query that gives me results of Organisation
So I am trying to write a single MySQL query to replace what is
I'm pretty new to MYSQL. I'm trying to write a query which will search
I'm currently trying to write a simple ORM with PHP and mysql. I want
I'm trying to figure out how to write a MySQL query that will return

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.