473,410 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

left join in case of....

Hey,

Suppose you have a table_ex like:

id nuber1 chr ( = a or b)
1 1000 a
2 1001 a
3 1002 b
4 1003 a
etc..

Now, is it possible to have a query like:

LEFT JOIN tablea if chr=a ON table_ex.number1=tablea.id
OR
LEFT JOIN tableb if chr=b ON table_ex.number1=tableb.id

Uhm.. .. or something like that :-)

Thanx in advance anyway..
Jul 20 '05 #1
1 3583
Quarco wrote:
Now, is it possible to have a query like:

LEFT JOIN tablea if chr=a ON table_ex.number1=tablea.id
OR
LEFT JOIN tableb if chr=b ON table_ex.number1=tableb.id


Yes, you can put other terms in the join condition, even if they aren't
strictly related to the columns on which the join is defined.

In your case, I infer that you're using chr to dictate which table a
given record in table_ex should join. I think you need to do two left
joins. The syntax could look like this:

SELECT ...
FROM table_ex T
LEFT JOIN tablea ON (T.number1 = tablea.id AND T.chr='a')
LEFT JOIN tableb ON (T.number1 = tableb.id AND T.chr='b');

In a way, you _are_ joining to both tablea and tableb for every record
in table_ex, but no rows in tablea/tableb match the condition for the
given record, if the second term in the condition is false. It's a
little bit hard to get one's brain wrapped around that concept, but it
works!

The parentheses are not required, I just though it makes it more clear
that both terms are part of the same condition.

Bill K.
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: michael | last post by:
Gotta post because this is driving me nuts. Trying to DELETE orphans. I can successfully: SELECT GroupID FROM Groups LEFT JOIN Users ON UsersID = UserID WHERE UsersID IS NULL; but when I...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
1
by: Paul Bramscher | last post by:
Here's one for pathological SQL programmers. I've got a table of things called elements. They're components, sort of like amino acids, which come together to form complex web pages -- as nodes...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
1
by: rossz | last post by:
I have this query to get the 10 most common products from in stock, based upon which parent category they are in: SELECT p.*,i.sql_inventory AS quantity FROM products AS p JOIN inv_cnt AS i ON...
3
by: Ian Boyd | last post by:
i know nothing about DB2, but i'm sure this must be possible. i'm trying to get a client to create a view (which it turns out is called a "Logical" in DB2). The query needs a LEFT OUTER JOIN, but...
5
by: Sascha.Moellering | last post by:
Hi, I receive the error code SQL0338N if I try to compile this statement (part of the statement): .... left outer join lateral (SELECT * FROM LIZSYSABA.VWZL0359TBS WHERE tbs_name = CASE WHEN...
2
dlite922
by: dlite922 | last post by:
I've heard this may not be possible, but never hurts to ask. I've got three tables, Case, Violator, Alias Case is a court police case Violator is the person Alias is different names...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.