Search code examples
phpmysqlmysqlisubquery

mysql query return boolean but I expect to return id


This is my query:

select id
from inquiry
where id = (select min(id) from inquiry where (id > 108 AND is_deleted=N))

What is wrong in this query? I'm expecting like in nested where id is compared to 108 and result will be greater than 108 but it returns boolean

calling function with parameters like nextIdUserInquiry(108, $con, 'N');

and whole function body is below

function nextIdUserInquiry($inquiryId, $conn, $cond) 
  { 
    echo "in function"; 
    $qry="select id from inquiry where id = (select min(id) from inquiry where (id > $inquiryId AND is_deleted=$cond))"; 
    echo $qry; 
    $result = $conn->query($qry); 
    $row = $result->fetch_assoc(); 
    echo $row["id"]; 
  } 

and mysql error is that

Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\besttour\admin\connect_db_test.php


Solution

  • the inner query returns a queryset, therefore you can't use ...where id=(select... try instead ...where id in (select...

    or even better try with the following:

    select id
    from inquiry 
    where (id > 108 AND is_deleted=N)
    order by id asc
    limit 1
    

    (tanks to P.Salmon)