help on SQL query

January 8, 2012 at 23:59:07
Specs: Windows 7
Hi,
As i am not much of an SQL person, would need some kind help

If anyone could provide some help in querying some data off 3 tables.

table1
=====
empid - surname - name - contact
---------------------------------
1 - a - z - 123
2 - b - y - 234
3 - c - x - 345


table2
=====
condid - empid
-------------
A1 - 1
A2 - 1
A3 - 1
A1 - 2
A3 - 2


table3
=====
condid - conddesc
A1 - employ
A2 - probation
A3 - Resign

what i would need is to have the query to show the report as

empid - surname - name - contact - condition1 - condition2 - condition3
----------------------------------------------------------------------
1 - a - z - 123 - employ - probation - resign
2 - b - y - 234 - employ - resign -
3 - c - x - 345 - - -


can any sql Gurus help?


See More: help on SQL query

Report •


#1
January 10, 2012 at 07:09:02
I'm no guru, but I can advise you to read up on the 'join' command. Check out mysqljoin.com

Report •

#2
January 10, 2012 at 12:52:07
SQL is very good at delivering sets of data. This outer join:

SELECT table1.empid, table1.surname, table1.name, table1.contact, table3.conddesc
FROM table1
LEFT OUTER JOIN
(table2
INNER JOIN table3
ON table2.condid = table3.condid)
ON table1.empid = table2.empid
ORDER BY table1.empid

delivers all the data required:

 empid surname name       contact    conddesc

     1 a       z          123        probation
     1 a       z          123        employ
     1 a       z          123        Resign
     2 b       y          234        employ
     2 b       y          234        Resign
     3 c       x          345

The desired format is a variable number of columns so set theory will not work in my opinion. A procedural language solution is required for once the data is obtained from SQL.

Not knowing your database, I cannot help you further. Let me know if you have any questions or comments.


Report •

Related Solutions


Ask Question