Jump to content

mysql condition (join) help


garethhall

Recommended Posts

Hi Guys

 

I have 2 tables

Table 1 called 'comp' (Holds company details) and has fields like

compID,

compName,

compPhone,

compAddress

 

Table 2 called compRel (Holds company relational data) has fields

compId,

subCompId

 

 

The sql I need to get all the right company id's is

SELECT 
   r.compId,
   r.subCompId
FROM
  compRel AS r
WHERE
  r.compId = 37
AND 
  r.subCompId = 37

And like I said this is all correct so far and outputs data like so

compId                subCompId

1                              37

37                            41

37                            48

37                            53

37                            66

 

 

So need to join the compName from the comp Table on to the id's but the hard part is that the first line needs to join comp.compName on the compRel.compId and rest need to join comp.compName on compRel.subcompId

 

I can't get this to work

 

My sql looks like this now

SELECT
  r.compId,
  r.subCompId,
  IF(r.subCompId = 37, r.compId, r.subCompId) AS rID,
  comp.compID,
  comp.compName
FROM
  compRel AS r
RIGHT JOIN
  comp
ON
  rID = comp.compID
WHERE
  r.compID = 37
AND
  r.subCompId = 37

 

So now I get a error "Unknown column rID"

 

The thing is if I change ON condition to r.subCompId = comp.compID and I look at my result rID is outputting the correct id's so why is rID not working?

Link to comment
Share on other sites

Use another JOIN. Really. You can JOIN the same table in twice. Use one of them for the parent/left side and the other for the child/right side.

SELECT
p.compID AS `parent`,
p.compName AS `parent_name`,
c.compID AS `child`,
c.compName AS `child_name`
FROM compRel
JOIN comp p ON compRel.compId = p.compID
JOIN comp c ON compRel.subCompId = c.compID
WHERE compRel.compId = 37 OR compRel.subCompId = 37

 

The reason yours doesn't work is because the aliasing is one of the last things MySQL does when it processes a SELECT query, and the WHERE clause is one of the first things. It doesn't know what "rID" is yet.

Link to comment
Share on other sites

Thank you for the reply but I really need the compName in only on column.

 

However I did solve the problem using UNION

 

For interest sake I have include the completed statemant

SELECT 
*
FROM 
compRel 
RIGHT JOIN 
comp 
ON 
compRel.subCompId = comp.compID
RIGHT JOIN 
compSettings 
ON 
comp.compID = compSettings.compId
WHERE 
compRel.compId = 37

UNION

SELECT 
*
FROM 
compRel 
RIGHT JOIN 
comp 
ON 
compRel.compId = comp.compID
RIGHT JOIN 
compSettings 
ON 
comp.compID = compSettings.compId
WHERE 
compRel.subCompId = 37
ORDER BY compName ASC

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.