Solved

help needed in sql statement

Posted on 2013-10-23
5
296 Views
Last Modified: 2013-10-23
hi EE

i have two tables "Employee" and "Phone"
Table Employee

EmployeeID | Name
_____________ |  ______
1                       AAA
2                        BBB

Table Phone

EmployeeID  PhoneNO
1                      123
1                       456
1                       789
2                         555

what i need that when i make sql statement between the two table i need the result to be like this

EmployeeID  PhoneNo
1                    123 , 456 ,789
2                      555

i dont want to get all the rows as the normal case

thanks
0
Comment
Question by:Kamal Khaleefa
5 Comments
 
LVL 10

Expert Comment

by:ienaxxx
ID: 39593569
if MySQL, you can use the GROUP_CONCAT function
0
 
LVL 10

Accepted Solution

by:
ienaxxx earned 300 total points
ID: 39593570
0
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 100 total points
ID: 39593595
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 100 total points
ID: 39593635
Here's one one to use 'for xml path'; using cross apply
SELECT
*
FROM employee
CROSS APPLY(
            SELECT STUFF(
                          (
                           SELECT ','+ cast(phone.PhoneNO AS varchar)
                           FROM    Phone
                           WHERE   employee.EmployeeID = phone.EmployeeID
                           FOR XML PATH('')
                           )
                         ,1,1,'')
            ) AS ca1 (phones)

Open in new window

it produces this result:
| EMPLOYEEID | NAME |      PHONES |
|------------|------|-------------|
|          1 |  AAA | 123,456,789 |
|          2 |  BBB |         555 |

Open in new window

0
 
LVL 16

Author Closing Comment

by:Kamal Khaleefa
ID: 39593654
thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now