[nycphp-talk] Mysql Selects
drydell at att.net
drydell at att.net
Tue Jun 21 16:48:13 EDT 2005
IN is a standard construct in every DB engine I can think of... IN expects a comma delimited list:
SELECT cols FROM table WHERE zip in (11111, 22222, 33333)
with PHP, you can do something like:
$zips = array(11111, 22222, 33333);
$rs = mysql_query('SELECT cols FROM table WHERE zip in ('.join(',', $zips).')');
-------------- Original message from "Chris Bielanski" <Cbielanski at inta.org>: --------------
> AFAIK, in MySQL <=4.x you'll have to do a code-aggregation:
> [PHP]
> $result = array();
> foreach ($zips as $key=>$val) {
> $result[] = $val;
> $rs = mysql_query("SELECT * FROM organizations WHERE org_zip = $val");
> if(@mysql_hum_rows($rs) != 0)
> while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
> $result[$val][] = $row;
> }
> }
>
> var_dump($result);
> [/PHP]
>
> In MySQL 5, you could do an IN SELECT or similar aggregation.
>
> Thanks,
> Chris Bielanski
> Web Programmer,
> International Trademark Association,
> 655 Third Avenue, 10th Floor
> New York, NY 10017-5617 USA
> +1 (212) 642-1745, f: +1 (212) 768-7796
> mailto:cbielanski at inta.org, www.inta.org
> INTA -- 125 Years of Excellence
>
>
> > -----Original Message-----
> > From: talk-bounces at lists.nyphp.org
> > [mailto:talk-bounces at lists.nyphp.org] On Behalf Of harvey
> > Sent: Tuesday, June 21, 2005 4:24 PM
> > To: NYPHP Talk
> > Subject: [nycphp-talk] Mysql Selects
> >
> > Hello,
> >
> > I've got code that queries the 'zipcode' tables in a database
> > and finds the zips that are within X miles of Y zip code. Works fine.
> >
> > > > $z = new zipcode_class;
> > $zips = $z->get_zips_in_range($zipcode,$distance);
> > if (empty($zips))
> > { echo "Error: " . $z->last_error; }
> > else { foreach ($zips as $key => $value) { echo
> > "$key is $value miles from
> > $zipcode.
"; }; } ?>
> >
> > Now I want to query the 'organization' tables and return all
> > the orgs that have one of the zips found above. But I'm not
> > sure how to go about it.
> > Something like the following (which I know is incorrect)?
> >
> > SELECT * FROM organizations WHERE org_zip IN ($zips)
> >
> > Thanks in advance for your help...I'm sure it's some very
> > basic concept I'm missing.
> >
> > Harvey
> >
> > _______________________________________________
> > New York PHP Talk Mailing List
> > AMP Technology
> > Supporting Apache, MySQL and PHP
> > http://lists.nyphp.org/mailman/listinfo/talk
> > http://www.nyphp.org
> >
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20050621/41593611/attachment.html>
More information about the talk
mailing list