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

  • SEARCH
  • Home
  • 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 8235727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:54:33+00:00 2026-06-07T18:54:33+00:00

Possible Duplicate: mysql is array in multiple columns I have two tables: Posts Table

  • 0

Possible Duplicate:
mysql is array in multiple columns

I have two tables:

Posts Table

PostID | Gallery
  1    | 4,7,8,12,13
  2    | 1,2,3,4
  3    | 5,8,9
  4    | 3,6,11,14

The values in Gallery are the primary keys in the Images table:

Images Table

ImageID | FileName
   1    | something.jpg
   2    | lorem.jpg
   3    | ipsum.jpg
   4    | what.jpg
   5    | why.jpg

The reason I do this instead of just adding a PostID key to the Images table is because those images can be associated with a lot of different posts. I suppose I could add another table for the relationships, but the comma-separated value is easier to work with as far as the jQuery script I am using to add to it.

If I’m on a page that requires the images associated with PostID 3, what kind of query can I run to output all of the FileNames for it?

  • 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-06-07T18:54:35+00:00Added an answer on June 7, 2026 at 6:54 pm

    You can use this solution:

    SELECT b.filename
    FROM posts a
    INNER JOIN images b ON FIND_IN_SET(b.imageid, a.gallery) > 0
    WHERE a.postid = 3
    

    SQLFiddle

    However, you should really normalize your design and use a cross-reference table between posts and images. This would be the best and most efficient way of representing N:M (many-to-many) relationships. Not only is it much more efficient for retrieval, but it will vastly simplify updating and deleting image associations.


    …but the comma-separated value is easier to work with as far as the jQuery script I am using to add to it.

    Even if you properly represented the N:M relationship with a cross-reference table, you can still get the imageid‘s in CSV format:

    Suppose you have a posts_has_images table with primary key fields (postid, imageid):

    You can use GROUP_CONCAT() to get a CSV of the imageid‘s for each postid:

    SELECT postid, GROUP_CONCAT(imageid) AS gallery
    FROM posts_has_images
    GROUP BY postid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: insert multiple rows via a php array into mysql I know about
Possible Duplicate: MySQL check if a table exists without throwing an exception I have
Possible Duplicate: MySQL query using an array How to use an array of values
Possible Duplicate: MySQL query using an array Passing an array to mysql I have
Possible Duplicate: PHP/MYSQL using an array in WHERE clause I have an array with
Possible Duplicate: php/Mysql query with inserting date fails I have a datepicker code below:
Possible Duplicate: MySQL GUI Programs I have installed MySql 5.5 on my PC (win7),
Possible Duplicate: Mysql single query join 3 table and get all the results Hi
Possible Duplicate: MySQL Results as comma separated list Combine Multiple child rows into one
Possible Duplicate: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result I have

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.