Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select all from one column if no variable is specified

Status
Not open for further replies.

darryncooke

Technical User
May 6, 2009
308
US
I have little (to maybe no) experience in sql.

I have a sql string that i want to create fora website using php where i have 3 criteria. Not all 3 will be selected.

lets say they are A, B, and C.

I can create a sql statement that pulls the URL variable using GET if A, B and C are specified. However if say only A is selected I want B and C to select ALL options. Is there a global sql (I know asterisk but that only works on slect all clums from what I have seen when I tried it) so say

SELECT * FROM table
WHERE column1 = A(or all) AND column2 = B(or all) AND column3 = C(or all)

where or all is the default if there is NO included url parameter specified for that variable.

Darryn Cooke
| The New Orange County Graphic designer and Marketing and Advertising Consultant
| Marketing and Advertising blog
 
how about ifnull

Code:
select * from table where column1 = ifnull(A,column1)

or you could build the sql dynamically based on the values of A,B,and C.

-----------------------------------------
I cannot be bought. Find leasing information at
 
Thats more of a php question. something like
Code:
<?php
$a = $_GET['A'];
$b = $_GET['B'];
...

$queryString = 'select * from table where 1=1 ';
if ($a != null && $a <> ''){
$queryString = $queryString . " and a = '" . $a . "'";
}
...
If my answer isn't sufficient, try posting in the PHP section.

-----------------------------------------
I cannot be bought. Find leasing information at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top