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: - SELECT * FROM associates
-
-
IF associates = 1
-
INNER JOIN table1 ON (table1.id = associates.jointblID )
-
ELSE
-
INNER JOIN table2 ON (table2.id = associates.jointtblID )
-
-
WHERE associatesID = X
Is that possible to do in one query?
Thanks :)
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.
Yeh, to give more details is always helpful, here goes :)
I have 3 tables like this:
table1 -
ID, type, typeID
-
-
1 1 27
-
2 1 13
-
3 2 89
-
table2 -
ID, name, x, x, x, x, x, x, x, x, x
-
1 xyz, x, x, x, x, x, x, x, x, x
-
2 zyx, x, x, x, x, x, x, x, x, x
table3 -
ID, name, x
-
1 xyz, x,
-
2 zyx, x,
My actual select would be like this -
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.
-
SELECT table1.type, table1.typeID,
-
CASE table1.type
-
WHEN '1' THEN (SELECT name FROM table2 WHERE table2.ID=table1.typeID)
-
WHEN '2' THEN (SELECT name FROM table3 WHERE table3.ID=table1.typeID)
-
END AS name
-
-
FROM table1
-
Ok, solved it I think, using the CASE statement above.
Is that a good way to do it?
Thanks
-
SELECT table1.type, table1.typeID,
-
CASE table1.type
-
WHEN '1' THEN (SELECT name FROM table2 WHERE table2.ID=table1.typeID)
-
WHEN '2' THEN (SELECT name FROM table3 WHERE table3.ID=table1.typeID)
-
END AS name
-
-
FROM table1
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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 ,...
|
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...
|
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...
|
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...
| |
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*...
|
by: lvpaul |
last post by:
Hello !
I habe 2 Tables
Table1: Orders
Fields: Ordernr, Opieces
Table2: Calloffs
Ordernr, Cpieces
|
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...
|
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')
...
|
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,...
|
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,...
| |
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...
|
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,...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |