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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:13:02+00:00 2026-06-06T22:13:02+00:00

Slightly unusual requirement here, which unfortunately is down to a poor table design a

  • 0

Slightly unusual requirement here, which unfortunately is down to a poor table design a long way back down the development path!

I have 3 tables, repairs, staff, technicians

The repairs table contains all the information on repair tasks booked in to my system. This contains a field “Technician” this field will contain the ID of either a staff member (from the table staff) or an ID of an outsourced (offsite) technician (from the table technicians), in this latter case the ID will have a prefix of “T”

So, due to this latter case prefix, I need to be able to grab that T and use it to determine whether my SQL query needs to JOIN table staff or table technicians

So, I have a fairly simple SQL Query:

SELECT technicians.screenName,

    repairs.turnAround, repairs.technician, repairs.dateIn, repairs.Type, 
    invoices.status, invoices.grossTotal 

    FROM repairs 
    LEFT JOIN invoices ON repairs.invNo=invoices.id 
    LEFT JOIN technicians ON technicians.id = REPLACE(repairs.technician, 'T','')

WHERE repairs.id ='REQUIRED JOB ID' ORDER BY repairs.dateIn DESC

This will work fine, and overcomes the “T” prefix for all cases where I have an outsourced technician.

BUT…

The IDs will mix up if I try to JOIN the staff table.

So I need a conditional join, such as:

  LEFT JOIN
      WHEN instr(repairs.technican,'T') > 0 THEN 
          JOIN TECHNICIANS TABLE
      ELSE
          JOIN STAFF TABLE
      END 

The further issue I can see here is that the field technicans.screenName being included in the field list will not work if I am not joining the technicians table, however, as the staff table includes a field screenName which I’d need if I joined that table, if I had an ambiguous field name screenName with no table prefix it should work shouldn’t it?

EDIT: I should probably add that the conditional join example above does NOT work!

  • 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-06T22:13:04+00:00Added an answer on June 6, 2026 at 10:13 pm

    You can join both tables with including/excluding INSTR condition, and show not null value then:

    SELECT COALESCE(technicians.screenName, staff.screenName) AS screenName,
    
    repairs.turnAround, repairs.technician, repairs.dateIn, repairs.Type, 
    invoices.status, invoices.grossTotal 
    
    FROM repairs 
    LEFT JOIN invoices ON repairs.invNo=invoices.id 
    LEFT JOIN technicians ON instr(repairs.technican,'T') > 0 AND technicians.id = REPLACE(repairs.technician, 'T','')
    LEFT JOIN staff ON instr(repairs.technican,'T') = 0 AND staff.id = repairs.technician
    
    WHERE repairs.id ='REQUIRED JOB ID' ORDER BY repairs.dateIn DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an slightly unusual security requirement, and I'm looking for advice on best
CoffeeScript list comprehensions are slightly different from Pythons... which of these is the way
A slightly archaic question I'm afraid but here goes: I have a program which
I have a requirement to build a slightly unusual data model, but I'm having
My Flash (AS3/AIR) application is currently using a slightly unusual architecture (for a Flash
Slightly daft, but... Is there a way to prevent Visual Studio treating a .jpg
I slightly remember that there is an class which is capable of stretching an
This answer had me slightly confused. What is a 'select to a temp table'
I use git for a slightly unusual purpose--it stores my text as I write
Slightly unorthodox question here: I'm currently trying to break an Apache with a handful

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.