Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique rows 1

Status
Not open for further replies.

newfriends1981

Programmer
Feb 17, 2007
3
IN
Hello all,

please conside the following table "photo"

id employee_id photo_name
---------------------------------------
1 2 abc.jpg
2 2 xyz.jpg
3 5 acd.jpg
4 2 sis.jpg
5 3 sfs.jpg
6 5 iii.jpg
7 2 ier.jpg
---------------------------------------

now what I want is this table should select all the fields with unique employee_id (i.e. query should select total 3 rows with employee id 2,3,5) and everythime the query should select different picture(which can be done using random() )

Can anyone help me with this problem?

Thank you,

newfriends1981
 
Hi

Code:
[b]select[/b]
employee_id,(
  [b]select[/b]
  photo_name
  [b]from[/b] photo
  [b]where[/b] p.employee_id=employee_id
  [b]order by[/b] random()
  [b]limit[/b] 1
) [b]as[/b] photo_name
[b]from[/b] photo p
[b]group by[/b] employee_id

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top