NoPHPknowldege
NoPHPknowldege

Reputation: 294

PHP and MYSQL displaying 1 record of multiple Name

i am designing one model website which have different events. I have store the image in a database table name image. Now my question is how do i display one record for one event out of many records? For example : the user will upload multiple events image in same image but with same event name. Now i have to display single image per event.

The name of table is images it got filed like imageID, imageName, imageType, eventName, imagePath

how do i create an sql statement where it print different event name and image

Upvotes: 0

Views: 1846

Answers (2)

Johan
Johan

Reputation: 76641

Expanding on the answer by @learner:

This code:

SELECT * FROM images  
WHERE eventName = 'name' 
GROUP BY eventName 

Selects a random row from all the possible rows that apply.
If you want some control over which image gets selected, you need to use a HAVING clause (or simular construct).

This code will select the image that was added most recently.

SELECT * FROM images  
WHERE eventName = 'name' 
GROUP BY eventName 
HAVING id = MAX(id)       <<-- assuming id is an auto_incrementing primary key
//HAVING date_added = MAX(date_added)  <<-- use this instead if you have an added_date

If you are only ever interested in a single image for a single event you can also do:

SELECT * FROM images
WHERE eventname = 'name'
ORDER BY id DESC
LIMIT 1 OFFSET 0;

Upvotes: 2

DEVOPS
DEVOPS

Reputation: 18790

Your question is not clear. Hope this query may help.

 select * from images 
 where eventName='name'
 group by eventName

Upvotes: 3

Related Questions