Query/SQL question

Posted on 2006-06-01
Last Modified: 2013-12-24
Something is wrong with my SQL in this query. I'd like to be able to include various WHERE conditions based on form input. Problem is, when a WHERE condition is included it returns 0 records. I've tested only the WHERE p.Active = 1 condition so far -- that should return 9425 records but returns 0. If I comment out the WHERE condition, it returns the correct number of total records, 9532.

select p.PK_PersonsID
      , concat(COALESCE(p.FirstName, ' '),' ',COALESCE(p.LastName, ' '), ' ',COALESCE(m.MBREADesignation, ' ')) as name
      , p.Company
      , p.Address1
      , p.Address2
      , p.City
      , p.State
      , p.County
      , p.Zip
      , p.Active
FROM tbl_persons p LEFT OUTER JOIN tbl_members m
    ON p.PK_PersonsID = m.PersonsID_FK
WHERE p.Active = 1
<cfif isDefined("form.sortCounty")>
and p.County = '#form.County#'</cfif>
<cfif isDefined("form.sortState")>
and p.State = '#form.State#'</cfif>
<cfif isDefined("form.sortMembership")>
      <cfif form.Membership EQ "All">and p.MemberType_FK > 0<cfelse>
      and p.MemberType_FK = #form.Membership#</cfif>
<cfif isDefined("form.sortorder")>
order by #form.printorder#</cfif>
Question by:brucecrabtree
    LVL 7

    Expert Comment

    If you are using SQL Server, you could run a Trace while your page executes, and see the actual syntax that is getting passed to the DB.

    Author Comment

    I'm using MySQL
    LVL 7

    Accepted Solution

    Try doing a <cfoutput> around your query instead of a <cfquery>, which will give you the sql code that is being generated.  Post the CFOUTPUT and we can go from there.

    Author Comment

    Thanks -- I didn't know that trick. That's incredibly useful! I can see what the problem is now.

    Featured Post

    Scale it in WD Gold

    With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

    Join & Write a Comment

    This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
    Article by: kevp75
    Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now