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

  • Home
  • SEARCH
  • 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 8485935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:47:34+00:00 2026-06-10T20:47:34+00:00

I am trying to get JOIN a table after I get a result from

  • 0

I am trying to get JOIN a table after I get a result from a SELECT with COALESCE

SELECT
    COALESCE(
            (
                SELECT
                      relocations.location_id
                FROM
                      relocations
                WHERE
                      parts.id=relocation.part_id
                ORDER BY
                      relocations.date_moved
                DESC
                LIMIT 
                      1
            )
            ,
            parts.location_id
       ) AS locationId,
    locations.name
FROM
    parts
JOIN
    locations ON locations.id=locationId

However the query is not valid and MySQL tells me locationId is an Unknown column. How could I fix this?

Table structure

[Parts]
id | location_id

[Relocations]
id | location_id | date_moved

[Locations]
id | name

  • 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-10T20:47:36+00:00Added an answer on June 10, 2026 at 8:47 pm

    If I understand correctly, you are trying to join on the COALESCE column. Try this:

    SELECT a.locationId, locations.name FROM
        (SELECT
            COALESCE(
                        (
                        SELECT
                              relocations.location_id
                        FROM
                              relocations
                        WHERE
                              parts.id=relocation.part_id
                        ORDER BY
                              relocations.date_moved
                        DESC
                        LIMIT 
                              1
                    )
                    ,
                    parts.location_id
               ) AS locationId
        FROM
            parts) a
    JOIN locations ON locations.id=a.locationId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get an id from a companies table where the id is
Trying to retrieve related data from the relationships table, after user clicks on the
I am trying to use my join table showing to get a list of
I'm trying to LEFT JOIN two tables, to get a list of all rows
I have a 3 tables that I'm trying to join and get distinct results.
I'm trying get the visible portion of UIImage from an UIImageView . UIImageView takes
I'm trying to extract data from a many to many table configuration. I'm sorry
Hey guys I just noticed that my join statement here SELECT * FROM reports
I'm trying to integrate custom table for authentication and role into PAS.plugin.sqlalchemy. After customisation
Simple question.. just can't get the result set in the order I need :p

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.