Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

limit using a string

I am using a query like this
$zip = "92331,92334, 92335";
//$zip could have one or more entries
$sql_query = "select * from $table where `CustomerZip` IN (".$zip.");
now what I want to do is put the limit on every zip
so if the
$zip = "92331, 92334";
I want to select 100 records from 92331 and 100 from 92334
I tried this query
$sql_query = "select * from $table where `CustomerZip` IN (".$zip.") limit 100;

but this selects the 100 results from 92331 and it stops. It doesnt do 100 from both. How would I do that
Thanks


0
syedasimmeesaq
Asked:
syedasimmeesaq
  • 6
  • 4
1 Solution
 
boowhupCommented:
$sql_query = "select TOP 100 * from $table where `CustomerZip` IN (".$zip.");
0
 
Renante EnteraSenior PHP DeveloperCommented:
Hi syedasimmeesaq,

Greetings!

You could simply try using the code as shown in the attach code snippet.

It should work like a charm.

Try it and goodluck.


Regards,
eNTRANCE2002 :-)
<?php	
	$zip = "92331,92334,92335";
 
	$sql = array();
	$temp = explode(",", $zip);
	foreach($temp as $val) {
		$sql[]= "
			SELECT * FROM $table
			WHERE `CustomerZip` = $val
			LIMIT 100
		";
	}
	
	$sql_query = implode("UNION ALL ", $sql);
?>

Open in new window

0
 
syedasimmeesaqAuthor Commented:
tried both. entrance2002: your method gives the same results as mine was, as I mentioned in my question above. boowhup: your method gives an error
thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Renante EnteraSenior PHP DeveloperCommented:
Hi syedasimmeesaq,

No, it should not return the same result as yours since the method I have given will produce an SQL statement with the UNION ALL.

In other words, it will returns all 100 records for each zip.  So, a total of 300 records will be returned in that example.

Kindly try to check and verify it.


Best regards,
eNTRANCE2002 :-)
0
 
syedasimmeesaqAuthor Commented:
entrance2002: I just recheck the code and its just returing 100 results from the first zipcode
it is not returning other zipcodes as you mentioned.

0
 
syedasimmeesaqAuthor Commented:
this is the query that it makes
SELECT * FROM table WHERE `CustomerZip` = 80233 LIMIT 100 UNION ALL SELECT * FROM table WHERE `CustomerZip` = 80234 LIMIT 100

and this is the mysq_num_rows

100

thanks
0
 
syedasimmeesaqAuthor Commented:
figured out
the query should have been like this
SELECT * FROM table WHERE `CustomerZip` = 80233 LIMIT 100 UNION ALL (SELECT * FROM table WHERE `CustomerZip` = 80234 LIMIT 100)

how do I put () around the other queries and not the first one
Thanks
0
 
syedasimmeesaqAuthor Commented:
I think I figured it out

this is how the query should have been

$sql[]= "
                      (  SELECT * FROM $table
                        WHERE `CustomerZip` = $val
                        LIMIT 100 )
                ";
        }
0
 
syedasimmeesaqAuthor Commented:
thanks!
0
 
Renante EnteraSenior PHP DeveloperCommented:
Oh, I see...

Anyway, change the code as shown in the snippet.

Hope it helps and goodluck again.
<?php	
	$zip = "92331,92334,92335";
 
	$sql = array();
	$temp = explode(",", $zip);
	foreach($temp as $key=>$val) {
		if($key > 0) {
			$sql[]= "
				(SELECT * FROM $table
				WHERE `CustomerZip` = $val
				LIMIT 100)
			";
		}
		else {
			$sql[]= "
				SELECT * FROM $table
				WHERE `CustomerZip` = $val
				LIMIT 100
			";		
		}
	}
	
	$sql_query = implode("UNION ALL ", $sql);
?>

Open in new window

0
 
Renante EnteraSenior PHP DeveloperCommented:
Oh, I thought that you will still have issues if the first "SELECT" statement is enclosed with "(" & ")".

Anyway, is everything working as expected now?
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now