views:

71

answers:

2

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?

A: 

Is that the final structure, or are there more pic columns ?

In other words, can a gallery have more than 3 pictures and if so, how's that represented ?

Russ C
Good points, but this should be a comment not an answer.
Mark Byers
No, each created gallery is limited to 3 pictures. But users can create more then one gallery.
Sergio
+4  A: 

I am assuming that each foid represents a gallery:

SELECT foid, uid, date,
       (CASE WHEN pic1 IS NULL THEN 0 ELSE 1 END +
        CASE WHEN pic2 IS NULL THEN 0 ELSE 1 END +
        CASE WHEN pic3 IS NULL THEN 0 ELSE 1 END
       ) AS pic_count
  FROM foto
 WHERE uid = $id
 ORDER BY foid DESC

Incidentally, this isn't a particularly sensible way to structure your schema. You really should split the pics out as a separate table:

foto:
foid | uid | date
-----------------------
104  |  5  | 2010-01-01
105  | 14  | 2009-04-08
106  | 48  | 2010-08-09

pic:
foid | pic
------------
104  | 1.jpg
104  | 2.jpg
104  | 3.jpg
105  | 8.jpg
106  | x.jpg
106  | y.jpg

Now, galleries can have more than three pics and querying is simpler:

SELECT foid, uid, date, COUNT(*)
  FROM foto
  JOIN pic USING (foid)
 WHERE uid = $id
 ORDER BY foid DESC
 GROUP BY foid, uid, date

EDIT: You can split an existing database thus (just guessing at stuff like column types):

CREATE TABLE picture (foid INT, pic VARCHAR(255));

INSERT INTO picture (foid, pic)
        SELECT foid, pic1 as pic FROM foto WHERE pic IS NOT NULL
        UNION
        SELECT foid, pic2 as pic FROM foto WHERE pic IS NOT NULL
        UNION
        SELECT foid, pic3 as pic FROM foto WHERE pic IS NOT NULL
    ;

ALTER TABLE foto
    DROP COLUMN pic1,
    DROP COLUMN pic2,
    DROP COLUMN pic3
    ;

Obviously, one should exercise considerable care when dropping columns, and make a backup before you start!

Marcelo Cantos
Thanks Marcelo. The problem is that now is to late to split the tables because there is more than 5000 records.Or you think that there is a way to do this?
Sergio
5000 records is nothing, and splitting it is quite easy (I'll update the answer). The usual pain-point is the amount of code you have to rewrite.
Marcelo Cantos
It will be great if there is an easy way to do this. Thanks!
Sergio
Thanks. I will do that.It won't take to much server resources if I query MySql like you said to get listing of all distinct users galleries and all pictures in it?This is more "server friendly" way to store and list images in MySql db?
Sergio
5000 * 3 records wouldn't even stress out MS Access.
Marcelo Cantos