473,513 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

If statment for join's?

6 New Member
Hello!

I've been trying to search for a resolution to this but can't see anything, so i must be being thick!

What I want to do is do a different join based on the result something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM associates
  2.  
  3.     IF associates = 1 
  4.            INNER JOIN table1 ON (table1.id = associates.jointblID )
  5.     ELSE 
  6.            INNER JOIN table2 ON (table2.id = associates.jointtblID )
  7.  
  8. WHERE associatesID = X
Is that possible to do in one query?

Thanks :)
Dec 31 '07 #1
4 1689
sierra7
446 Recognized Expert Contributor
I'm a Newbie to MySQL but I would not attempt an 'IF' in any SQL statement (so shall watch to see if I can learn something!)

I think you need a 'UNION' query where you combine the results of two seperate SELECT statement, each with their own conditions.

Both queries must return the same number of columns, so you may need to add dummy columns to one query.

Your example does not call for any fields (columns) from Table1 or Table2, so is pointless as it stands.

I shall watch with interest for an Expert response.
Jan 2 '08 #2
sym0ng
6 New Member
Yeh, to give more details is always helpful, here goes :)

I have 3 tables like this:

table1
Expand|Select|Wrap|Line Numbers
  1. ID, type, typeID
  2.  
  3. 1   1    27
  4. 2   1    13 
  5. 3   2    89
  6.  
table2
Expand|Select|Wrap|Line Numbers
  1. ID, name, x, x, x, x, x, x, x, x, x
  2. 1   xyz, x, x, x, x, x, x, x, x, x
  3. 2   zyx, x, x, x, x, x, x, x, x, x
table3
Expand|Select|Wrap|Line Numbers
  1. ID, name, x
  2. 1   xyz, x, 
  3. 2   zyx, x,
My actual select would be like this

Expand|Select|Wrap|Line Numbers
  1. SELECT table1.ID, table1.type, table1.typeID, tableX.name FROM table1, (then either join table2 or table3 as tableX matching table2/3 ID with table1.typeID)
I currently have the above working via PHP, but doing 2 queries, my aim of this is more trying to cut the queries of my whole website down by doing things like the above.
Jan 2 '08 #3
sym0ng
6 New Member
Expand|Select|Wrap|Line Numbers
  1. SELECT table1.type, table1.typeID,
  2. CASE table1.type
  3.     WHEN '1' THEN (SELECT name FROM table2 WHERE table2.ID=table1.typeID)
  4.     WHEN '2' THEN (SELECT name FROM table3 WHERE table3.ID=table1.typeID)
  5. END    AS name
  6.  
  7. FROM table1
  8.  
Ok, solved it I think, using the CASE statement above.

Is that a good way to do it?

Thanks
Jan 2 '08 #4
amitpatel66
2,367 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. SELECT table1.type, table1.typeID,
  2. CASE table1.type
  3.     WHEN '1' THEN (SELECT name FROM table2 WHERE table2.ID=table1.typeID)
  4.     WHEN '2' THEN (SELECT name FROM table3 WHERE table3.ID=table1.typeID)
  5. END    AS name
  6.  
  7. FROM table1
  8.  
Ok, solved it I think, using the CASE statement above.

Is that a good way to do it?

Thanks
yes, if it is giving you the desired result and the query performance is good then your solution is good.
Jan 2 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
937
by: Joe Saliba | last post by:
Hi, would like to know how to write a crosstable select statment in sql server2000 where having: - ItemNumber, ItemDescription, ItemColor, ItemSize as rows - Stores as columns - Qty * Netttc...
2
1389
by: gimme_this_gimme_that | last post by:
Can this statement be translated into DB2 ? CREATE VIEW RELEASE_REPORTS AS (SELECT RI.GROUP_ID AS GROUP_ID, RI.RELEASE_ID AS RELEASE_ID, RI.TARGET_REL_ID, RI.REL_DROP, B.DROPCOUNT ,...
5
3314
by: 73blazer | last post by:
Hello, I'm looking for a SQL syntax to put a variable name into the from clause. Specifically I have a colume in a table that is table name, and I want to use that result in the from clause in a...
2
1866
by: Dyslexic Fingars | last post by:
Warning I'm new to anything other than basic SQL so this 'might' seem like a daft question. My problem is - I've have two tables one hold's photo details and is called 'PhotoTable' it is linked...
6
1664
by: Liz Malcolm | last post by:
Hello and Thanks in advance for any help. I am using Access 2000. I have a data entry form that opens a main form (using the On Click event of the combo box ) with tab controls and 1 subform on...
1
1729
by: Me, Myself, and I | last post by:
First off, i apologize if my terminology is off... I am currently in a project that is basically a front-end to a database. In coding this, I am taking into account that it has the *potential*...
8
1514
by: lvpaul | last post by:
Hello ! I habe 2 Tables Table1: Orders Fields: Ordernr, Opieces Table2: Calloffs Ordernr, Cpieces
1
1574
by: zeebiggie | last post by:
Good morning I have a form with the controls in the insert statment below. table1 has an Auto increment primary key hence is omitted in the insert statment and form. Am getting the error It didnt...
2
2845
by: rshivaraman | last post by:
CREATE TABLE RS_A(ColA char(1), ColB varchar(10)) INSERT INTO RS_A VALUES ('S', 'shakespeare') INSERT INTO RS_A VALUES ('B', 'shakespeare') INSERT INTO RS_A VALUES ('P', 'shakespeare') ...
0
7265
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
7388
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
7545
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
5692
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,...
1
5095
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...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.