select statement woes

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default select statement woes

    I hope some one can help me figure this out. I've got a search feature for
    images and users can enter up to 3 terms and I'm not getting the results I need.

    I have 3 tables.
    1 - images - holds basic info about the images
    2 - keywords - holds a list of keywords that all clients share and can add to
    3 - keyword relationship table - links images with the keywords associated to
    it

    I also set up three images to test with and they have these keywords:

    image1 - ski, snow, mountain, terrain
    image2 - ski, snow, mountain'
    image3 - ski

    single word searches are fine, but if I search for 2-3 terms I get a blank
    result.

    Any help would be greatly appreciated. Thanks


    <cfquery name="GetResults" datasource="#Datasource#">
    SELECT DISTINCT I.ClientID, I.Imagename, I.imageNumber, I.ImageVert,
    I.ImageHorz
    FROM images I
    INNER JOIN image_Keyword_Rel R ON I.imageID = R.imageID
    AND R.ClientID = #ClientID#
    INNER JOIN keywords K ON K.keywordID = R.keywordID
    WHERE K.Keyword = '#term1#'
    AND K.Keyword = '#term2#'
    AND K.Keyword = '#term3#'
    ORDER BY I.SortOrder
    </cfquery>

    megalith Guest

  2. Similar Questions and Discussions

    1. if statement woes
      I am creating a list of competition standings. Based on the placement, 1st, 2nd, 3rd, I am setting the RGB to gold , silver, bronze, or nothing at...
    2. SP with Select statement
      Hi, I'm trying to select fileds that are in the results of a SP. So I have the table "tblItem" itemID int Identity Key, itemName varchar...
    3. help with SELECT statement
      "Aaron" <abroadway@ameritrust.com> wrote in message news:05a601c365df$b31a8d40$a401280a@phx.gbl... "SUM(ABS(action_date>= {" & start_date2 & "}...
    4. Getting column name in Select statement
      How can I use the column_name provided from information_schema.columns in a Select statement that is used for extracting certain columns from a...
    5. SELECT statement
      I have 3 tables: table countryPrice: productID countryId price 1 Italy 90 1 England ...
  3. #2

    Default Re: select statement woes

    I don't know if this will work but it may be worth a try. Its hard to tell
    without having access to your schema and data. I used a three way join the
    kewyords table as a subquery. You could probably use the INTERSECT command for
    a more graceful query but I'm using MySQL which doesn't support INTERSECT.

    I assume you have imageID as a foreign key in your keywords table:





    <cfquery name="GetResults" datasource="#Datasource#">
    SELECT DISTINCT I.ClientID, I.Imagename, I.imageNumber, I.ImageVert,
    I.ImageHorz
    FROM images I

    INNER JOIN image_Keyword_Rel R ON I.imageID = R.imageID
    AND R.ClientID = #ClientID#

    INNER JOIN keywords K ON K.keywordID = R.keywordID

    WHERE K.imageID IN
    (
    SELECT k1.imageID
    FROM Keywords k1, Keywords k2, Keywords k3

    WHERE 1 = 1
    AND k1.imageID = k2.imageID
    AND k2.imageID = k3.imageID

    <cfif Len(Trim(term1))>
    AND k1.keyword = '#term1#'
    </cfif>
    <cfif Len(Trim(term2))>
    AND k2.keyword = '#term2#'
    </cfif>
    <cfif Len(Trim(term3))>
    AND k3.keyword = '#term2#'
    </cfif>
    )

    ORDER BY I.SortOrder
    </cfquery>

    LeftCorner Guest

  4. #3

    Default Re: select statement woes

    Originally posted by: megalith

    I hope some one can help me figure this out. I've got a search feature for
    images and users can enter up to 3 terms and I'm not getting the results I need.

    I have 3 tables.
    1 - images - holds basic info about the images
    2 - keywords - holds a list of keywords that all clients share and can add to
    3 - keyword relationship table - links images with the keywords associated to
    it

    I also set up three images to test with and they have these keywords:

    image1 - ski, snow, mountain, terrain
    image2 - ski, snow, mountain'
    image3 - ski

    single word searches are fine, but if I search for 2-3 terms I get a blank
    result.

    Any help would be greatly appreciated. Thanks


    <cfquery name="GetResults" datasource="#Datasource#">
    SELECT DISTINCT I.ClientID, I.Imagename, I.imageNumber, I.ImageVert,
    I.ImageHorz
    FROM images I
    INNER JOIN image_Keyword_Rel R ON I.imageID = R.imageID
    AND R.ClientID = #ClientID#
    INNER JOIN keywords K ON K.keywordID = R.keywordID
    WHERE K.Keyword = '#term1#'
    AND K.Keyword = '#term2#'
    AND K.Keyword = '#term3#'
    ORDER BY I.SortOrder
    </cfquery>

    Change this:
    WHERE K.Keyword = '#term1#'
    AND K.Keyword = '#term2#'
    AND K.Keyword = '#term3#'

    to this
    WHERE K.Keyword = '#term1#'
    or K.Keyword = '#term2#'
    or K.Keyword = '#term3#'

    or this
    WHERE K.Keyword in( '#term1#', '#term2#', '#term3#')



    Dan Bracuk Guest

  5. #4

    Default Re: select statement woes

    LeftCorner,

    Thanks for the sample code. I don't have the imageID in the keywords table.
    I have a third table called image_Keyword_Rel that has the imageID and the
    KeywordID. I didn't want the keywords table to contain duplicate keywords and
    the way it's setup the images can share keywords and the images and keywords
    are connected thru the relationship table.

    I tried changing your code a bit to get it to work but didn't have any luck.
    Maybe you can suggest another way to write it?

    thanks



    --------------------------------------------------------------------------------
    -----------------
    Dan,

    Your suggestion of using "or" instead of and is how I have it running now but
    it returns to many irrelevant records. If someone is looking for "ski and
    Mountain and snow" I need results to be returned that have ALL three of those
    terms. The "or" gives me any combination.

    I also tried this "WHERE K.Keyword in( '#term1#', '#term2#', '#term3#')" as
    you suggested but MySQL 4.1 didn't seem to like it.

    any other suggestions would be appreciated, thanks

    megalith Guest

  6. #5

    Default Re: select statement woes

    Here is how my tables are setup:

    images (imageID, clientID, imagename, imagenumber, ImageVert, ImageHorz)
    image_Keyword_Rel (imageFKID, ImageID, KeywordID)
    Keywords (keywordID, keyword)
    megalith Guest

  7. #6

    Default Re: select statement woes

    This is just a SWAG, but is it close to what you are trying to do?

    Phil

    <cfquery name="GetResults" datasource="#Datasource#">
    SELECT DISTINCT I.ClientID, I.Imagename, I.imageNumber, I.ImageVert,
    I.ImageHorz
    FROM images I
    INNER JOIN image_Keyword_Rel R ON I.imageID = R.imageID
    AND R.ClientID = #ClientID#
    WHERE
    EXISTS (SELECT 1
    FROM keywords K
    WHERE K.keywordID = R.keywordID
    AND K.Keyword = '#term1#')
    <cfif IsDefined "term2>
    AND EXISTS (SELECT 1
    FROM keywords K
    WHERE K.keywordID = R.keywordID
    AND K.Keyword = '#term2#')
    </cfif>
    <cfif IsDefined "term3>
    AND EXISTS (SELECT 1
    FROM keywords K
    WHERE K.keywordID = R.keywordID
    AND K.Keyword = '#term3#')
    </cfif>
    ORDER BY I.SortOrder
    </cfquery>

    paross1 Guest

  8. #7

    Default Re: select statement woes

    Phil,

    I tried your code and it didn't return any results. I searched for
    "ski,terrain,snow" for which there is one image associated to those keywords.

    Your code did work with only one term submitted though, but not two or three.

    Any other ideas?

    Thanks for helping!

    megalith Guest

  9. #8

    Default Re: select statement woes

    Hmmmm....perhaps this?

    Phil


    <cfquery name="GetResults" datasource="#Datasource#">
    SELECT DISTINCT I.ClientID, I.Imagename, I.imageNumber, I.ImageVert,
    I.ImageHorz
    FROM images I
    WHERE
    EXISTS (SELECT 1
    FROM keywords K
    INNER JOIN image_Keyword_Rel R ON K.keywordID = R.keywordID
    AND R.ClientID = #ClientID#
    WHERE I.imageID = R.imageID
    AND K.Keyword = '#term1#')
    <cfif IsDefined "term2>
    AND EXISTS (SELECT 1
    FROM keywords K
    INNER JOIN image_Keyword_Rel R ON K.keywordID = R.keywordID
    AND R.ClientID = #ClientID#
    WHERE I.imageID = R.imageID
    AND K.Keyword = '#term2#')
    </cfif>
    <cfif IsDefined "term3>
    AND EXISTS (SELECT 1
    FROM keywords K
    INNER JOIN image_Keyword_Rel R ON K.keywordID = R.keywordID
    AND R.ClientID = #ClientID#
    WHERE I.imageID = R.imageID
    AND K.Keyword = '#term3#')
    </cfif>
    ORDER BY I.SortOrder
    </cfquery>

    --or--

    <cfquery name="GetResults" datasource="#Datasource#">
    SELECT DISTINCT I.ClientID, I.Imagename, I.imageNumber, I.ImageVert,
    I.ImageHorz
    FROM images I
    WHERE
    EXISTS (SELECT 1
    FROM keywords K, image_Keyword_Rel R
    WHERE K.keywordID = R.keywordID
    AND I.imageID = R.imageID
    AND R.ClientID = #ClientID#
    AND K.Keyword = '#term1#')
    <cfif IsDefined "term2>
    AND EXISTS (SELECT 1
    FROM keywords K, image_Keyword_Rel R
    WHERE K.keywordID = R.keywordID
    AND I.imageID = R.imageID
    AND R.ClientID = #ClientID#
    AND K.Keyword = '#term2#')
    </cfif>
    <cfif IsDefined "term3>
    AND EXISTS (SELECT 1
    FROM keywords K, image_Keyword_Rel R
    WHERE K.keywordID = R.keywordID
    AND I.imageID = R.imageID
    AND R.ClientID = #ClientID#
    AND K.Keyword = '#term3#')
    </cfif>
    ORDER BY I.SortOrder
    </cfquery>

    paross1 Guest

  10. #9

    Default Re: select statement woes

    Phil,

    The last one did it!
    Thank you so much for your time on this I really appreciate it.

    Bart
    megalith Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139