Solved

limit using a string

Posted on 2009-05-14
11
255 Views
Last Modified: 2012-05-07
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
Comment
Question by:syedasimmeesaq
  • 6
  • 4
11 Comments
 
LVL 5

Expert Comment

by:boowhup
ID: 24392183
$sql_query = "select TOP 100 * from $table where `CustomerZip` IN (".$zip.");
0
 
LVL 14

Accepted Solution

by:
Renante Entera earned 500 total points
ID: 24392187
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24392237
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
 
LVL 14

Expert Comment

by:Renante Entera
ID: 24392257
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24392270
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24392391
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24392418
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24392431
I think I figured it out

this is how the query should have been

$sql[]= "
                      (  SELECT * FROM $table
                        WHERE `CustomerZip` = $val
                        LIMIT 100 )
                ";
        }
0
 
LVL 9

Author Closing Comment

by:syedasimmeesaq
ID: 31581783
thanks!
0
 
LVL 14

Expert Comment

by:Renante Entera
ID: 24392440
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
 
LVL 14

Expert Comment

by:Renante Entera
ID: 24392462
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
count download link and run update query 9 54
PHP script redirecting to URL not working on AWS EC2 after Elastic IP 12 60
Php pie charts 3 26
WordPress Themes 10 34
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now