I have a table for image gallery with four columns like:
foid | uid | pic1 | pic2 | pic3 | date |
-----------------------------------------------
104 | 5 | 1.jpg | 2.jpg | 3.jpg | 2010-01-01
105 | 14 | 8.jpg | | | 2009-04-08
106 | 48 | x.jpg | y.jpg | | 2010-08-09
Mysql query for the user’s galleries looks like:
SELECT * FROM foto WHERE uid = $id order by foid DESC
The thing that I want to do is count the number of images (PIC1, PIC2, PIC3) in every of the listed galleries.
What is the best way for doing that?
I am assuming that each
foidrepresents a gallery:Incidentally, this isn’t a particularly sensible way to structure your schema. You really should split the pics out as a separate table:
Now, galleries can have more than three pics and querying is simpler:
EDIT: You can split an existing database thus (just guessing at stuff like column types):
Obviously, one should exercise considerable care when dropping columns, and make a backup before you start!