Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using If /else if with sql queries

Author  Topic 

ebanbury
Starting Member

1 Post

Posted - 2011-03-29 : 04:54:28
Hi
I'm having big difficulties using If statements within my sql queries. I'm using dreamweaver and have information being inserted into tables via recordsets.

However I dont'seem able to edit the queries to include an 'if 'statement.

What I am actually trying to do is the following:
I have several fields from a form, posting data
One of the fields is a list of property areas/zones.
However the first avaialble option in the dropdown list is 0
I would therefore like the search to include the option of bringing back all the records based on the other form fields, but ignoring the district_zone field - i.e. where district_zone NOT LIKE '0'. The first part of bringing back selected records is easy, but not to bring back all the records.

I am also getting a bit mixed up with the syntax, as I have to mix sql with php.


For example:

if ($_POST[district_zone] = '0') {
$query_rs_district = "SELECT district_zone, district_name FROM property_districts WHERE district_zone NOT LIKE '0'"; }
else {
$query_rs_district = "SELECT district_zone, district_name FROM property_districts WHERE district_zone = '$_POST[district_zone]'";}



or I am also trying something like this:

query = "Select property_districts.district_name, property_districts.district_zone, property_details.prop_type FROM property_districts, property_details
WHERE property_districts.district_zone = property_details.add_zone
if ($_POST [district_name]= '0') {
query = query }
else {
query = query AND district_zone NOT LIKE '0'
}

Any help would be much appreciated.

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-03-29 : 05:48:04
Are you getting an error? or just the wrong results?
Is "district_zone NOT LIKE '0'" really a condition that gives you everything?

this is a wild shot... but how about:


if ($_POST[district_zone] = '0') {
$query_rs_district = "SELECT district_zone, district_name FROM property_districts WHERE district_zone NOT LIKE '0'"; }
else {
$query_rs_district = "SELECT district_zone, district_name FROM property_districts WHERE district_zone = '$_POST[district_zone]'";}


or

$query_rs_district = "SELECT district_zone, district_name FROM property_districts WHERE '0'='$_POST[district_zone]' or district_zone = '$_POST[district_zone]'";


Corey

snSQL on previous signature "...Oh and by the way Seventhnight, your signature is so wrong! On so many levels, morally, gramatically and there is a typo!"
Go to Top of Page
   

- Advertisement -