简体   繁体   中英

join two tables in one with one id and multiple ids in another table

I have a table content that is joined with content_image . There can be different images for a single content_id . I want to join them with case, according to image_size .

In other words, I have one content, with 3 different images sizes. But these images are in another table, so I want to show only one content, with 3 different images in one output.

Anyway hope the code will be more clear in my explanation:

    SELECT C.CHAPTER_ID,CH.CHAPTER,C.CONTENT_ID,
           C.CONT_HEAD AS NAME,
           SMALL=(CASE WHEN CI.IMAGE_SIZE=0 THEN CONTIMAGE_SMALL END),
           BIG=(CASE WHEN CI.IMAGE_SIZE=2 THEN CONTIMAGE_SMALL END),
           C.UPDATE_DATE,
           C.RECORD_DATE
    FROM   CONTENT C
               LEFT OUTER JOIN CONTENT_CHAPTER CH ON C.CHAPTER_ID = CH.CHAPTER_ID
               LEFT OUTER JOIN CONTENT_IMAGE CI ON CI.CONTENT_ID=C.CONTENT_ID
    WHERE  CH.CONTENTCAT_ID = 14
    ORDER BY C.UPDATE_DATE DESC,C.RECORD_DATE DESC

and the output code:

    <cfoutput query="get_images">
        <a class="highslide" target="_blank" onclick="return hs.expand(this, { slideshowGroup: 1 } )" href="/documents/content/#big#"><img src="/documents/content/#small#" title="#name#" border="0" /></a>
    </cfoutput>

The problem is this generates two outputs. In first one, only the big value is populated. In the second one, only the small value is populated. If I group the cfoutput by content_id , then it does generate a single result, but again - only the big value is defined :) But I want a single output with both big and small values defined.

Thank you all for help!

show only one content with 3 different images in one output.

If I am understanding correctly, you are trying to return a single record with both image size. To achieve that result you must JOIN to your image table multiple times: once for each of the image sizes.

Again as Dan mentioned above , if you wish to preserve the OUTER JOIN's any filtering on those tables must be done within the JOIN 's, not the WHERE clause, otherwise you end up doing an implicit INNER JOIN instead.

 SELECT C.CHAPTER_ID
       , CH.CHAPTER
       , C.CONTENT_ID
       , C.UPDATE_DATE
       , C.RECORD_DATE
       , C.CONT_HEAD AS NAME
       , CIS.CONTIMAGE_SMALL AS SMALLImage
       , CIB.CONTIMAGE_SMALL AS BIGImage
 FROM CONTENT C
         LEFT OUTER JOIN CONTENT_CHAPTER CH 
             ON C.CHAPTER_ID = CH.CHAPTER_ID AND CH.CONTENTCAT_ID = 14
         LEFT OUTER JOIN CONTENT_IMAGE CIS 
             ON CIS.CONTENT_ID = C.CONTENT_ID AND CIS.IMAGE_SIZE = 0
         LEFT OUTER JOIN CONTENT_IMAGE CIB 
             ON CIB.CONTENT_ID = C.CONTENT_ID AND CIB.IMAGE_SIZE = 2
ORDER BY C.UPDATE_DATE DESC,C.RECORD_DATE DESC

Let's start with this:

FROM CONTENT C
LEFT OUTER JOIN CONTENT_CHAPTER CH ON C.CHAPTER_ID = CH.CHAPTER_ID
LEFT OUTER JOIN CONTENT_IMAGE CI ON CI.CONTENT_ID=C.CONTENT_ID
WHERE CH.CONTENTCAT_ID = 14

It looks ok but it's not. When you refer to an outer joined table in your where clause, it effectively becomes an inner join. The fix is to move that filter to your from clause, like this:

FROM CONTENT C
LEFT OUTER JOIN CONTENT_CHAPTER CH ON C.CHAPTER_ID = CH.CHAPTER_ID
and CH.CONTENTCAT_ID = 14

LEFT OUTER JOIN CONTENT_IMAGE CI ON CI.CONTENT_ID=C.CONTENT_ID

Next, you can use the group attribute of the cfoutput tag to manipulate the display of your recordset. For the query above, you would want something like this.

<cfoutput query="yourquery" group="content_id">
#content_id#
<cfoutput>
#big# #small#
</cfoutput>
</cfoutput>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM