Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 15th, 2004, 02:27 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default Users selection driven search query...

When a user makes choices on a search page a query is created based on their selections, in the example below there are three criteria that arrive in form variables.

What is the best way to create the query? As you can see I have a 'WHERE 1=0' in there to create a false-true scenario. This seems a bit of a hack?

A minimum of one of the three criteria will be selected, since we don’t know which one how do we know which conditional statement to place the AND (this would eliminate the need for the 'WHERE 1=0' part of the query)

NOTE: there are no errors in the query it runs fine
OBJECTIVE: I would like to get rid of the 'WHERE 1=0'

SQL = "Select DISTINCT products.ID, products.description,Products.forder FROM products LEFT OUTER JOIN ProductProfiles ON products.id = productprofiles.productId " & _
    "LEFT OUTER JOIN users ON productProfiles.prodManager = users.ID " & _
    "WHERE 1=0 "
    if request.form("pn") = "on" then
       sql = sql & " OR (Products.description LIKE '%" & SearchString & "%') "
    end if
    if request.form("sd") = "on" then
       sql = sql & " OR (ProductProfiles.Description LIKE '%" & SearchString & "%') "
    end if
    if request.form("nd") = "on" then
       sql = sql & " OR (Products.natDescription LIKE '%" & SearchString & "%') "
    end if
    sql = sql & " AND productProfiles.RegionID = " & IRegion & " "
    sql = sql & " ORDER BY Products.forder"

Thank you in advance

Wind is your friend
Matt
__________________
Wind is your friend
Matt
 
Old November 15th, 2004, 03:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Matt,

As you say atleast one of those 3 criteria would be imposed always. It could be removed(1=0) and used as below.
Code:
SQL = "Select DISTINCT products.ID, products.description,Products.forder FROM products LEFT OUTER JOIN ProductProfiles ON products.id = productprofiles.productId " & _
    "LEFT OUTER JOIN users ON productProfiles.prodManager = users.ID "
    sql = sql & " AND productProfiles.RegionID = " & IRegion & " AND "

    Dim strWhere
    strWhere = ""
    if request.form("pn") = "on" then
       strWhere = strWhere & " (Products.description LIKE '%" & SearchString & "%') "
    end if
    if request.form("sd") = "on" then
       if len(strWhere) > 0 then strWhere = strWhere & " OR "
       strWhere = strWhere & " (ProductProfiles.Description LIKE '%" & SearchString & "%') "
    end if
    if request.form("nd") = "on" then
       if len(strWhere) > 0 then strWhere = strWhere & " OR "
       strWhere = strWhere & " (Products.natDescription LIKE '%" & SearchString & "%') "
    end if
    sql = sql & " ORDER BY Products.forder"
    I haven't tested this. This might need slight alterations if needed.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old November 15th, 2004, 06:24 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

happygv

ahhh:
;;;if len(strWhere) > 0 then strWhere = strWhere & " OR "
Was the seed for the thought train I was stumbling over

Whats your thoughts on my: false-true 'WHERE 1=0' method, is this a hack?

Thanking you


Wind is your friend
Matt
 
Old November 16th, 2004, 03:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Matt,

I wont say that is a hack. That is another way of achieving it. IMO both the way of approach are fine. WHERE 1=0 is just a work around to achieve it in another way. There shouldn't be any issues in going that way at all.

Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
query name search dstein4d Access VBA 1 August 13th, 2007 12:19 PM
Data Driven Query Task forkhead SQL Server DTS 1 June 22nd, 2006 11:01 AM
Query based on combo box selection help Elain Access 1 January 3rd, 2006 11:33 PM
Data Driven Query Update Row Problem Hardache SQL Server DTS 1 June 10th, 2004 08:42 AM
Query to populate top users flyin ADO.NET 2 April 2nd, 2004 11:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.