jc_ply Posted March 20, 2008 Share Posted March 20, 2008 Hey I'm new to the forums and have just begun getting to grips with PHP to enhance my flash work. Anyway spent the last 2 weeks tackling it and im to the point where I'm a little confused on which way to go. Basically i have a table which looks like this id | name | img1 | img2 | img3 | 1 dave 3 5 3 the 3 5 3 are integers which will stand for image ids selected by the user and i need to find the most popular id and return the value via an integer to be picked up by flash (loadVars). So I need to search a column e.g "num1" find the most popular id and add up how many instances there are of it. My initial thoughts were towards arrays and loops but not too sure if im missing something being new to server side scripts. I know I should probably separate the images and use a relational database but time is short, and my coding abilities are not too hot at the moment. I'd appreciate any help and hope to become more involved on the forum in the future. Thanks, Jason Quote Link to comment https://forums.phpfreaks.com/topic/97032-searching-columns-mysql/ Share on other sites More sharing options...
sasa Posted March 20, 2008 Share Posted March 20, 2008 if you want to find most popular id in one column use SELECT `img1`, COUNT(`id`) AS c FROM `imgs` GROUP BY `img1` ORDER BY c DESC if you want to find most popular id in all columns use SELECT imgs.img1 as img,count(distinct(i1.id)) as c FROM `imgs`,imgs as i1 where imgs.img1 in(i1.img1,i1.img2,i1.img3) group by imgs.img1 union SELECT imgs.img2 as img,count(distinct(i1.id)) as c FROM `imgs`,imgs as i1 where imgs.img2 in(i1.img1,i1.img2,i1.img3) group by imgs.img2 union SELECT imgs.img3 as img,count(distinct(i1.id)) as c FROM `imgs`,imgs as i1 where imgs.img3 in(i1.img1,i1.img2,i1.img3) group by imgs.img3 order by c desc or reorganize your db Quote Link to comment https://forums.phpfreaks.com/topic/97032-searching-columns-mysql/#findComment-496572 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.