![]() | ![]() | ||||||||
| | #1 |
| Digital Carpenter War Room Member Join Date: Aug 2012 Location: Pennsylvania
Posts: 855
Thanks: 170
Thanked 209 Times in 174 Posts
|
Im using the following query which returns a correct result, however it only returns one result when I know for sure there is more than one in the radius. Any advice would be appreciated. SELECT DISTINCT zip, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM zips HAVING distance < 40 |
| | |
| | |
| | #3 |
| Digital Carpenter War Room Member Join Date: Aug 2012 Location: Pennsylvania
Posts: 855
Thanks: 170
Thanked 209 Times in 174 Posts
|
Well Im sure that there are more distinct rows, but I did notice this, when I take the MSQL resource and change it to an array and print_r it then it shows one result, if I change it to an assoc array and iterate through and extract it shows all the results. So I got it working now but would still like to know why it only shows all the results with an assoc array and extracting.
|
| | |
| | |
| | #4 | |
| HyperActive Warrior War Room Member Join Date: Jun 2012
Posts: 138
Thanks: 15
Thanked 26 Times in 25 Posts
| Quote:
| |
| | |
| | #5 |
| Digital Carpenter War Room Member Join Date: Aug 2012 Location: Pennsylvania
Posts: 855
Thanks: 170
Thanked 209 Times in 174 Posts
| |
| | |
| | |
| | #6 |
| Active Warrior Join Date: Oct 2012
Posts: 94
Thanks: 13
Thanked 12 Times in 11 Posts
|
undoubtedly remove DISTINCT. DISTINCT selects only exclusive field names for all fields after the DISTINCT. e.g. if you are searching for through same ZIP, the result will be a single line, because all the other ZIP codes after first row are repeating. The same rule is applied on your formula, it should be DISTINCT. If you really need to use DISTINCT, first think over which field it's applied, then perform 2 mysql queries one over another to the same DB as in example SELECT DISTINCT 'the unique field name' FROM (SELECT zip, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM zips HAVING distance < 40) WHERE 'your condition here' Warning: the fields in the second select should be included into first select Hope wrote not too complex |
| | |
| | #7 | |
| HyperActive Warrior War Room Member Join Date: Jun 2012
Posts: 138
Thanks: 15
Thanked 26 Times in 25 Posts
| Quote:
With GROUP BY it will only get 1 result per unique ZIP grouping all of the same kind which seems to be the goal here I believe. | |
| | |
| | #8 | |
| Digital Carpenter War Room Member Join Date: Aug 2012 Location: Pennsylvania
Posts: 855
Thanks: 170
Thanked 209 Times in 174 Posts
| Quote:
For example Code: =mysql_fetch_array() print_r() where Code: while (=mysql_fetch_assoc()){
extract();
echo "<br>";
} edit: LOL the forum pulls out the varables even when typed on a code block. Thats about a big pain in the rear in a coding forum. | |
| | ||
| | |
| | #9 |
| Software Engineer War Room Member Join Date: Nov 2008 Location: Asheville, NC USA
Posts: 626
Thanks: 33
Thanked 89 Times in 81 Posts
| Yeah, they've never fixed that. You can prevent it yourself though. When replying, scroll down a bit (below the editor), to the section titled "Additional Options". Uncheck the parts starting with "Automatically...". One of those messes up code.
|
| I build web things, server things. I help build the startup Veenome.
| |
| | |
| | #10 |
| Senior Warrior Member War Room Member Join Date: Apr 2006 Location: , , USA.
Posts: 2,866
Thanks: 55
Thanked 225 Times in 208 Posts
|
Of course mysql_fetch_assoc is pulling in all the rows, based on what you posted you have it in a while loop and the mysql_fetch_array isn't in a loop.
|
| | |
| | #11 |
| Digital Carpenter War Room Member Join Date: Aug 2012 Location: Pennsylvania
Posts: 855
Thanks: 170
Thanked 209 Times in 174 Posts
| |
| | |
| | |
| | #12 |
| HyperActive Warrior War Room Member Join Date: Jun 2012
Posts: 138
Thanks: 15
Thanked 26 Times in 25 Posts
|
Essentially GROUP BY lets you use aggregate operators (MAX, SUM, GROUP_CONCAT, ..., or a HAVING clause). DISTINCT just removes duplicates.
|
| | |
| | #13 | |
| Senior Warrior Member War Room Member Join Date: Apr 2007 Location: Caldwell, Idaho, USA.
Posts: 1,966
Thanks: 454
Thanked 1,066 Times in 685 Posts
| Quote:
Code: $row[] = mysql_fetch_assoc( $result ); $row[] = mysql_fetch_assoc( $result ); $row[] = mysql_fetch_assoc( $result ); $row[] = mysql_fetch_assoc( $result ); $row[] = mysql_fetch_assoc( $result ); $row[] = mysql_fetch_assoc( $result ); $row[] = mysql_fetch_assoc( $result ); $row[] = mysql_fetch_assoc( $result ); Code: while ( $r = mysql_fetch_assoc( $result ) ) {
$row[] = $r;
} | |
| | |
| | #14 |
| Digital Carpenter War Room Member Join Date: Aug 2012 Location: Pennsylvania
Posts: 855
Thanks: 170
Thanked 209 Times in 174 Posts
| LOL I know, thanks for the tip though, have a look at my last post. This entire thread was the result of a 19 hour coding session and me being brain fried and half out of it. Id delete it if I knew how.
|
| | |
| | |
![]() |
|
| Bookmarks |
| Tags |
| msql, query |
| Thread Tools | |
| |
![]() |