Current location: Hot Scripts Forums » General Community » Script Requests » dynamic where section of sql query


dynamic where section of sql query

Reply
  #1 (permalink)  
Old 01-31-08, 11:38 AM
soloWebDev soloWebDev is offline
Wannabe Coder
 
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
dynamic where section of sql query

the title does not do this question justice, but i wasn't sure how to sum up my question.
EXAMPLE:
i have a sql query with a where stmt
i have a form submit that a user submits x amount of invoices per one activity
knowing that x will be different every time, and that your sql query needs to encapsulate all of the "x's" with a
WHERE (x1 = .. OR x2 = .. OR x3 = ..)
but the next time it could be
WHERE (x1 = .. OR x2 = .. )
and one more
WHERE (x1 = .. OR x2 = .. OR x3 = .. OR x4 = ..)
and so on...
has anyone done this where the where stmt would be altered on the fly pending on the account and activity you are looking at?
END EXAMPLE:
THOUGHT:
have a query, but another query included at the same time or something like that.

Thank you in advance, I hope my description has been helpful for what I am looking for.
__________________
We will see what happens next.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 01-31-08, 02:14 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
If this is using a scripting language, like PHP, the query string can be built several ways -

By concatenating the pieces together using a loop.

If the terms x1 = ..., x2 = ... were in an array and they always used one type of condition (OR in your example) you could implode() them and than concatenate that with the rest of the query.

If the column is actually always the same WHERE x1 = 123 OR x1 = 456..., then you can use a mysql IN() function (similar to a php in_array() function) in the WHERE clause.

So, short answer, yes. A scripting language can dynamically build any valid query string using a variable number of items to put in the WHERE clause.

Your example is a bit vague. For example, if you really have a number of columns in a row - x1, x2, x3, ... holding similar data, that would be a bad table design and would require a lot more code to insert, find, modify, and retrieve any piece of data.

If you could show at least two different actual queries to clarify what is actually occurring, someone can provide some specific code.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 01-31-08, 03:29 PM
soloWebDev soloWebDev is offline
Wannabe Coder
 
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Mab, your explanation is amazing. thank you for taking the time and explaining that to me.
links for future viewers
SQL IN(
PHP implode()
Def: Concatenation
__________________
We will see what happens next.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 01-31-08, 03:48 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
No problem, that is what the ProgrammingTalk forum is here for and what it does best.

Hmm... now where is that thread [SOLVED] button and the ThankYou button....
(just a little behind the scenes ribbing...)
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Query Paramater Precedence? Rahil PHP 2 09-03-06 02:20 PM
2 flash websites for sale metamorph General Advertisements 5 01-09-05 11:03 PM
Declared Functions skipper23 PHP 4 12-17-03 11:06 AM
index page not showing up skipper23 PHP 3 12-15-03 02:10 PM
change my field in this example sal21 ASP 3 07-14-03 03:49 AM


All times are GMT -5. The time now is 01:20 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.