5 Replies - 317 Views - Last Post: 18 May 2013 - 02:26 PM Rate Topic: -----

#1 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Multiples Joins in different tables.

Posted 07 May 2013 - 10:10 AM

Hello guys,

I will be concrete. I have the following tables: [All connected together]

Clients   | Products | Others       | Employees    
ID        | ID       | ID           | ID
desc      | desc     | idemployeeBy | name
idproduct |          | idemployeeTo |
idother   | 


I want to start selecting the ID, plus product description.
SELECT clients.ID, products.desc

So far, so good. Now I need to select the employeesBy and employeesTo 'name', acording to the fields of others table that has a "connection" with clients table.

So I thought of doing this:
SELECT clients.ID, P.description, E.name AS 'Open by', E1.name AS 'Assigned To'
FROM clients
INNER JOIN others ON others.id = clients.id 
LEFT JOIN products AS P ON P.ID = clients.idprodut
LEFT JOIN employees AS E ON E.ID = others.idemployeeBy 
LEFT JOIN employees AS E1 ON E1.ID = others.idemployeeTo 
GROUP BY others .id, clients.ID, P.description, E.name, E1.name 
ORDER BY clients.ID DESC

I don't know if I'm not explaining properly..?

Am I doing this correctly? I can't test in SQL Query right now, only tomorrow.
Thank you guys.

This post has been edited by Keylogger: 07 May 2013 - 11:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Multiples Joins in different tables.

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9393
  • View blog
  • Posts: 35,273
  • Joined: 12-June 08

Re: Multiples Joins in different tables.

Posted 07 May 2013 - 10:11 AM

Quote

I don't know if I'm not explaining properly..?

Yeah, run that by again...
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3623
  • View blog
  • Posts: 12,513
  • Joined: 12-December 12

Re: Multiples Joins in different tables.

Posted 07 May 2013 - 11:28 AM

SELECT clients.ID, P.description, E.name AS 'Open by', E1.name AS 'Assigned To'
FROM clients
INNER JOIN others ON others.id = clients.id 
LEFT JOIN products AS P ON P.ID = clients.idproduto
LEFT JOIN employees AS E ON E.ID = others.idreceivedby 
LEFT JOIN employes AS E1 ON E1.ID = others.idassignedto
GROUP BY others .id, clients.ID, P.description, E.name, E1.name 
ORDER BY clients.ID DESC

There are MANY discrepancies between this statement and the table/field names that you list before this. If you want assistance you should correct all these: if there are already errors in the question then the answer will be even more difficult to find :whistling:
Was This Post Helpful? 0
  • +
  • -

#4 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Multiples Joins in different tables.

Posted 07 May 2013 - 11:40 AM

You guys are right. I always change the values when I come here, like the name of the fields etc..so you guys can understand better :D

I can't test it now. Tomorrow I will give you guys an answer. For now, thanks.
Was This Post Helpful? 0
  • +
  • -

#5 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Multiples Joins in different tables.

Posted 08 May 2013 - 09:15 AM

It worked nicely. Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Multiples Joins in different tables.

Posted 18 May 2013 - 02:26 PM

You'll want to read my answer to your other post, which will give you some information that will be helpful in streamlining this query as well. For example, your GROUP BY clause is superfluous.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1