Connecting Tech Pros Worldwide Forums | Help | Site Map

Can this be done with a query

YoBro
Guest
 
Posts: n/a
#1: Jul 20 '05
Lets say I wanted to select all employees and list their occupations.
I have this query:
SELECT * FROM employees, job WHERE employees.id=job.id

Now some employees might have more than one job. I use PHP to loop through
the results and get this:

Bob
- Driver
Bob
- Cleaner
Dave
- Writer
Mike
- Director
Mike
- Producer

How do I group it to output like this, and can it be done in the query?
Bob
- Driver
- Cleaner
Dave
- Writer
Mike
- Director
- Producer

FYI, My PHP
while($row=mysql_fetch_array($sql)){
echo "$row[name]<br>";
echo " - $row[job]<br>";
}

Appreciate any feedback.

Yobro





sks
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Can this be done with a query



"YoBro" <yobro@wazzup.co.nz> wrote in message
news:DNXZc.21406$N77.933372@news.xtra.co.nz...[color=blue]
> Lets say I wanted to select all employees and list their occupations.
> I have this query:
> SELECT * FROM employees, job WHERE employees.id=job.id[/color]

You didn't specify what the id column is, but assuming its the primary key,
why are you joining these tables on the primary key ?

Assuming each employee can have more than one job and each job can only be
assigned to one employee, then you want something like

select * from employees e join job j on e.id=j.employee order by e.name


Closed Thread