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 3230626
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:56:56+00:00 2026-05-17T16:56:56+00:00

This one has been haunting me for quite a while now.. I have been

  • 0

This one has been haunting me for quite a while now..
I have been developing my own CMS using a MySQL database; each uploaded image is assigned to a category, according to which part of the site it is related to (I need to do this since each category has its own way to handle images).

I have several tables for the various entities, an ‘images’ table, and an associative table: ‘images_assoc’, their basic structure is as follows:

CREATE TABLE `images` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(50) NOT NULL default '',
  `link` varchar(255) NOT NULL default '',
  `idcategory` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `idcategory` (`idcategory`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
INSERT INTO `images` (`id`, `name`, `link`, `idcategory`) VALUES (1, 'some name', 'foo.jpg', 1);
CREATE TABLE `images_assoc` (
  `id` int(11) NOT NULL auto_increment,
  `idimage` int(11) NOT NULL default '0',
  `idelement` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `idimage` (`idimage`,`idelement`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
INSERT INTO `images_assoc` (`id`, `idimage`, `idelement`) VALUES (1, 1, 2);
CREATE TABLE v`some_entity` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(250) NOT NULL,
  `description` text NOT NULL,
  -- some other data
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

What I need to do, in the various pages of the site, is to retrieve a list of the page elements together with their related image(s). I have not yet been able to do it with one single select. What I am doing right now is to run a select for the page elements and then run a query for each single element to retrieve any associated image, with a query like this:

SELECT i.id, i.link, i.name
FROM images_assoc AS ia, images AS i
WHERE ia.idelement = '1'
AND i.idcategory = '1'
AND i.id = ia.idimage

one solution I initially came up with was:

SELECT t. * , i.id, i.link, i.name
FROM (
 (
  (
   contents AS t
  )
  LEFT JOIN images_assoc AS ia ON t.id = ia.idelement
 )
 LEFT JOIN images AS i ON i.id = ia.idimage
)
WHERE i.idcategory = '1'
AND i.id = ia.idimage

but it left out any element with no associated image, which is the exact contrary of the purpose of the left join.
Later, I tried changing the query to this:

SELECT t. * , i.id, i.link, i.name
FROM (
 (
  (
   contents AS t
  )
  LEFT JOIN images_assoc AS ia ON t.id = ia.idelement
 )
 LEFT JOIN images AS i ON ( i.id = ia.idimage
 AND i.idcategoriy = '1' )
)

But still, the query is faulty: I end up with a cross-join-like result, since the category restriction is applied later..

Does anyone have any suggestions?
Any tips regarding the database structure are welcome as well..

  • 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-17T16:56:57+00:00Added an answer on May 17, 2026 at 4:56 pm

    well your idcategory condition can never match unless there is a corresponding counterpart in the other table, thats why your results with no corresponding image “disappear”, left join is behaving correctly.

    try this:

    sql query:

    SELECT some_entity.title, some_entity.description, some_entity.id as entityid, images.id as imageid, images.link, images.name
    FROM 
    some_entity 
    LEFT JOIN images_assoc ON (some_entity.id = images_assoc.idelement)
    LEFT JOIN (SELECT * FROM images WHERE idcategory=1) images ON (images.id = images_assoc.idimage)
    

    or probably better (the obe only to illustrate why it didnt work):

    SELECT some_entity.title, some_entity.description, some_entity.id AS entityid, images.id AS imageid, images.link, images.name
    FROM some_entity
    LEFT JOIN images_assoc ON ( some_entity.id = images_assoc.idelement ) 
    LEFT JOIN images ON ( images.id = images_assoc.idimage ) 
    WHERE images.idcategory =  '1' OR images.idcategory IS NULL 
    

    test data:

    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    CREATE TABLE IF NOT EXISTS `images` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(50) NOT NULL DEFAULT '',
      `link` varchar(255) NOT NULL DEFAULT '',
      `idcategory` int(11) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`),
      KEY `idcategory` (`idcategory`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=124 ;
    CREATE TABLE IF NOT EXISTS `some_entity` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `title` varchar(250) NOT NULL,
      `description` text NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=322 ;
    CREATE TABLE IF NOT EXISTS `images_assoc` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `idimage` int(11) NOT NULL DEFAULT '0',
      `idelement` int(11) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`),
      KEY `idimage` (`idimage`,`idelement`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
    
    INSERT INTO `images` (`id`, `name`, `link`, `idcategory`) VALUES
    (123, 'some name', 'foo.jpg', 1);
    INSERT INTO `images_assoc` (`id`, `idimage`, `idelement`) VALUES
    (1, 123, 321);
    INSERT INTO `some_entity` (`id`, `title`, `description`) VALUES
    (321, 'test', 'test');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one has been bugging me for a while now. Is there a way
This one has been bugging me for a while now, but I never seem
This one has been stumping me for a while. But I'm no expert. This
This one has been driving me nuts for a few days now and I've
This one has been puzzling my for some time now. Let's imagine a class
This one has me beat; I have a WPF window with two (important for
This one has us all baffled at work. We have two services running on
This one has been driving me crazy today. Since upgrading to Grails 1.2 and
I feel like this one has been asked before, but I'm unable to find
There's this one thing in C++ which has been making me feel uncomfortable for

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.