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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:19:01+00:00 2026-06-17T22:19:01+00:00

I have the following 3 tables: mysql> explain runners; +————-+——————+ | Field | Type

  • 0

I have the following 3 tables:

mysql> explain runners;
+-------------+------------------+
| Field       | Type             |
+-------------+------------------+
| runner_id   | int(10) unsigned |
| name        | varchar(100)     |
+-------------+------------------+

mysql> explain runs;
+-------------+------------------+
| Field       | Type             |
+-------------+------------------+
| run_id      | int(10) unsigned |
| runner_id   | int(10) unsigned |
| race_id     | int(10) unsigned |
| position    | int(10)          |
+-------------+------------------+

mysql> explain races;
+-------------+------------------+
| Field       | Type             |
+-------------+------------------+
| race_id     | int(10) unsigned |
| date        | datetime         |
+-------------+------------------+

Next, I have to display all the races a given runner participated in, something like this:

mysql> select runner.name, runs.run_id, runs.position, races.race_id
       from runners
       join runs on(runs.runner_id = runners.runner_id)
       join races on(races.race_id = run.run_id)
       where runners.runner_id = 1;
+------------------+--------+----------+---------+
| name             | run_id | position | race_id |
+------------------+--------+----------+---------+
| John Runner      |      1 |        1 |       1 |
| John Runner      |      7 |        3 |       2 |
| John Runner      |     15 |        2 |       3 |
................... more lines ...................
+------------------+--------+----------+---------+

Now I should add another column called “win/second” to this query, that will display the name of the runnner that finished second if the current runner’s position = 1 or display the name of the winning runner in that race if the current runner’s position != 1, but I just don’t know how to get this working or even what to start with.

  • 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-17T22:19:02+00:00Added an answer on June 17, 2026 at 10:19 pm

    This should get you what you need:

    select runners.name, 
       runs.run_id, 
       runs.position, 
       races.race_id,
       case
          when runs.position = 1
          then    runnerssecond.Name
          else    runnerswinner.Name
       end as NextColumn
    from runners
       join runs on(runs.runner_id = runners.runner_id)
       join races on(races.race_id = runs.Race_Id)
       left join runs runswinner on runswinner.race_id = races.race_id and runswinner.Position = 1
       left join runners runnerswinner on runnerswinner.Runner_Id = runswinner.Runner_Id
       left join runs runsecond on runsecond.race_id = races.race_id and runsecond.Position = 2
       left join runners runnerssecond on runnerssecond.Runner_Id = runsecond.Runner_Id
    where runners.runner_id = 1;
    

    And the SQL Fiddle.

    Good luck.

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

Sidebar

Related Questions

I have the following two tables in my mysql database. Table Name: groups id
I have the following mysql query: EXPLAIN SELECT id, name, title, description, time FROM
I have the following tables: create temporary table Items (item_id int, item_name varchar(10)); create
I have the following query: mysql> explain SELECT Exhibition.venue_id, Exhibition.name, Exhibition.slug, Venue.name, Venue.slug, Venue.location_id,
Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT
I have the following destination , image and destination_image tables: (MYSQL): destination: id_destination image:
I have three mysql table from same database Db1. Three tables have following columns.
I have 2 mysql tables : Question with the following columns : id, question,
I have 2 mysql tables 1. questions: with the following columns: id, title, answer1,
I have the following MySQL table structure: num field company phone website 1 Gas

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.