How is the AND clause used in mysql queries?

71 Views Asked by At

I have the below code that works fine up to the AND clause. The portion of code below that with a SELECT *, in front of the ( 3959, works fine too. I tried putting the AND clause to marry them together but it did not work.

Is it possible with the computation part to combine the WHERE, AND, and AS? I thought AND would do the trick but it doesn't. Any ideas?

<?php
    $miles = '25'; //your search radius

    $resul = mysqli_query($con,"SELECT memlocid, LocLat, LocLong
    FROM matmemmatrix 
    inner JOIN matloc
    ON matloc.MLocID = matmemmatrix.memlocid
    WHERE `$a` = '1' AND
    

    ( 3959 * acos( cos( radians('$mylat') ) * 
    cos( radians( LocLat ) ) * 
    cos( radians( LocLong ) - 
    radians('$mylong') ) + 
    sin( radians('$mylat') ) * 
    sin( radians( LocLat ) ) ) ) 
    AS distance FROM matloc HAVING distance < '$miles' ORDER BY 
    distance ASC LIMIT 0, 5");

EDITED "Tried" using a subquery (first time)

<?php
$miles = '25'; //your search radius

$resul = mysqli_query($con,"SELECT MLocID, LocLat, LocLong, ( 3959 * acos( cos( radians('$mylat') ) * 
cos( radians( LocLat ) ) * 
cos( radians( LocLong ) - 
radians('$mylong') ) + 
sin( radians('$mylat') ) * 
sin( radians( LocLat ) ) ) )
AS distance FROM matloc HAVING distance < '$miles' ORDER BY distance ASC LIMIT 0, 5
WHERE 
    `$a` IN (SELECT 
     memlocid
    FROM
     matmemmatrix
     WHERE `a`='1')");

EDITED with no Sub Query

<?php
$miles = '25'; //your search radius

$resul = mysqli_query($con,"SELECT MLocID, LocLat, LocLong, ( 3959 * acos( cos( radians('$mylat') ) * 
cos( radians( LocLat ) ) * 
cos( radians( LocLong ) - 
radians('$mylong') ) + 
sin( radians('$mylat') ) * 
sin( radians( LocLat ) ) ) )

AS distance FROM matloc HAVING distance < '$miles' ORDER BY distance ASC LIMIT 0, 5
inner JOIN matmemmatrix
ON matloc.MLocID = matmemmatrix.memlocid
WHERE `$a` = '1'");

Both have errors after the "AS" clause. I tried adding commas and parentheses. And if that is the problem I didn't find the right combo. I did change the first table to matloc since that is the table where all of the select information is located. matmemmatrix is the table with all the columns I will be checking if "1". There will be a couple hundred columns that I will check if "1".

Here is my error code after I changed it to what might looks to be working. The new code I tried is below this new error message.

Fatal error: Uncaught mysqli_sql_exception: This version of MariaDB
 doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' in
 C:\xampp\htdocs\mydb\tempdistance.php:22 Stack trace: #0 
C:\xampp\htdocs\mydb\tempdistance.php(22):
 mysqli_query(Object(mysqli), 'SELECT MLocID, ...') #1 {main}
 thrown in C:\xampp\htdocs\mydb\tempdistance.php on line 22

Newest attempt that produced the message above:

<?php
$miles = '25'; //your search radius

$resul = mysqli_query($con, "SELECT MLocID, LocLat, LocLong
FROM matloc
inner JOIN matmemmatrix
ON matloc.MLocID = matmemmatrix.memlocid
WHERE `$a` = '1' IN 
    (SELECT ( 3959 * acos( cos( radians('$mylat') ) * 
cos( radians( LocLat ) ) * 
cos( radians( LocLong ) - 
radians('$mylong') ) + 
sin( radians('$mylat') ) * 
sin( radians( LocLat ) ) ) ) 
AS distance FROM matloc HAVING distance < '$miles' 
ORDER BY distance ASC LIMIT 0, 5)");
0

There are 0 best solutions below