Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 765153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:50:37+00:00 2026-05-14T16:50:37+00:00

I have a table for image gallery with four columns like: foid | uid

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T16:50:37+00:00Added an answer on May 14, 2026 at 4:50 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gradient image as a background for a table header(9x18pix). This image
I have table with 50 entries (users with such details like Name Surname Location
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have an image gallery that is created using a repeater control. The repeater
I have CakePHP app in which I'd like to attach gallery to multiple resources.
Here is a simplified version of my database model. I have two tables: Image,
I have table inside a div tab. The table has 40 rows in it
I have table rows of data in html being filled from a CGI application.
While trying to use LINQ to SQL I encountered several problems. I have table
I have a table with more than a millon rows. This table is used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.