Jump to content

Searching columns (mySQL)


jc_ply

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.