Jump to content

Can I combine these queries into one


AdRock

Recommended Posts

I have a search form with a field for keywords (which uses a full text search on 4 columns), a drop down list and another text field where a user can enter a time.

 

At the minute, I can search using keywords, search by the drop down list, or search by the time field or I can leave them all blank and return all the results but i can't do any of them together

 

I want to be able to use all the fields or just some of them to filter the results but i'm having trouble.

 

If a text field is empty or there is no value selected from the drop down when the form is posted, i want it to ignore those fields in the database.

 

How would i combine all the queries so i can do it all at once or is there a more efficent way of doing this maybe by a switch statement

    if(!empty($address)) {
    	$result = mysql_query("SELECT id,userid,seats_available,start_street,start_postcode,start_lat,start_long,
    end_street,end_postcode,end_lat,end_long,TIME_FORMAT(depart_time,'%H:%i') as depart, 
    MATCH(start_street, start_postcode, end_street, end_postcode) AGAINST('$address' IN BOOLEAN MODE)
            as score FROM carshare WHERE MATCH(start_street, start_postcode, end_street, end_postcode) 
    AGAINST('$address*' IN BOOLEAN MODE) ORDER BY score ASC LIMIT $offset,$entries_per_page");
    }
    else if(!empty($seats)) {
    	$result = mysql_query("SELECT id,userid,seats_available,start_street,start_postcode,start_lat,start_long,
    end_street,end_postcode,end_lat,end_long,TIME_FORMAT(depart_time,'%H:%i') as depart FROM carshare WHERE seats_available='$seats' ORDER BY id ASC LIMIT $offset,$entries_per_page");
    }
    else if(!empty($departure)) {
    	$result = mysql_query("SELECT id,userid,seats_available,start_street,start_postcode,start_lat,start_long,
    end_street,end_postcode,end_lat,end_long,TIME_FORMAT(depart_time,'%H:%i') as depart FROM carshare WHERE depart_time='$departure'
    ORDER BY id ASC LIMIT $offset,$entries_per_page");
    }
    else {
    	$result = mysql_query("SELECT id,userid,seats_available,start_street,start_postcode,start_lat,start_long,
    end_street,end_postcode,end_lat,end_long,TIME_FORMAT(depart_time,'%H:%i') as depart FROM carshare ORDER BY id ASC LIMIT $offset,$entries_per_page");
    }

Link to comment
https://forums.phpfreaks.com/topic/97017-can-i-combine-these-queries-into-one/
Share on other sites

the question of combining queries is

 

1) Do these 2 queries pull data  linked by a single field i.e GROUP BY UserID

2) Do I Pull data for the same thing in these queries?

3) Are they at all related

4) Does combining reduce the amount of data manipulation I need to do in php?

 

If you answer yes you can combined

I had an idea but i don't know if it's possible.

 

Can I check to see if the variable to be selected from the database is empty and if it is don't add it to the query in the where clause otherwise selected from the database.

 

If i try and select a field with no value in the variable it won't return any rows.

 

The only way i can think of is doing it this way i have done but that seems unnecessary as I should be able to use one query and only add to the WHERE clause any fields that are to be searched

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.