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

  1. #1

    Default Sub Query problem

    I have one simple question,
    this is my query:

    SELECT users.*, (SELECT TOP 1 Product_name FROM Products WHERE user_id =
    users.user_id) AS Product
    FROM users

    Query is working fine, it returns all users and products which are tied to
    users, problem is when I am filtering data by product (subquery) I don't want
    all users only users which are tied to product.

    Did anyone had this before, thanks

    Buda123 Guest

  2. Similar Questions and Discussions

    1. Query problem, please help.
      mysql Ver 12.22 Distrib 4.0.24, for pc-linux-gnu (i386) gives me: The following database Error occured: You have an error in your SQL syntax....
    2. ***Sql Query problem
      Randy Webb wrote: SELECT DISTINCT OrderID FROM trans WHERE Trans_ID = 1 AND OrderID NOT IN (SELECT DISTINCT OrderID FROM trans WHERE Trans_ID =...
    3. Query of Query problem
      Error Executing Database Query. Query Of Queries runtime error. Table named "DATA" was not found in Memory. It is misspelled, or the table is...
    4. Query on Query and CF casting problem
      I am using a custom tag in MX7 that was working fine in 5 that renders a table. The input to the custom tag is a query and it's columns along with...
    5. query problem
      SELECT top5.PRODID, p.PRODNAME FROM ( SELECT TOP 5 SALES = COUNT(*), PRODID FROM SALES GROUP BY PRODID ORDER BY SALES DESC ) AS top5 INNER JOIN...
  3. #2

    Default Re: Sub Query problem

    Hmmm, if I am reading this right, I would say that you would want to add a a
    parameter to the where clause of your sub-select restricting your query to a
    specific product, something like this:

    SELECT users.*,
    (SELECT TOP 1 Product_name
    FROM Products
    WHERE user_id = users.user_id
    AND Product_name = '#productname#') AS Product
    FROM users

    You could make it optional with <cfif> tags so that if your #productname# is
    blank, or doesn't exist, you return users for all products.

    Phil

    paross1 Guest

  4. #3

    Default Re: Sub Query problem

    Thanks Phil,
    I wanted to avoid <if> statements in loop since I have 6-7 filters, I wanted
    to solve everithing inside query. I got the solution, last night was long :),

    Here it is if anybody need it in the future:

    SELECT users.*, (SELECT TOP 1 Product_name FROM Products WHERE user_id =
    users.user_id <cfif isDefined('Form.bill_end_date')>AND bill_end_date <
    #CreateODBCDate(Form.bill_end_date)#</cfif>) AS Product
    FROM users
    <cfif isDefined('Form.bill_end_date')>
    WHERE user_id IN (SELECT user_id FROM Products <cfif
    isDefined('Form.bill_end_date')>WHERE bill_end_date <
    #CreateODBCDate(Form.bill_end_date)#</cfif>)
    </cfif>

    Buda123 Guest

  5. #4

    Default Re: Sub Query problem

    I meant by using <cfif> tags within your query, which is exactly what you ended up doing. :D

    Phil
    paross1 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