Search code examples
mysqlsqlmysql-error-1111

error in sql query


I am writing an query in sql and getting an error:

Invalid use of group function

What does it mean? In my query, where clause is given below:

select c.name,s.contact,s.number
from list c , senior s
where c.id = s.id AND c.name = 'Abg' AND c.state ='qw' AND MIN(c.dob);

Basically, I have 2 files and I need to find the younger customer from 2nd file and then have to retrieve its data from first file. I have the ID number of customers in 2nd file. I first check the ids with the id of first file. And check its state and name. And then I need to find younger among those customers.Thats Why i need MIn function.


Solution

  • You need to use a subquery:

     select c.name,s.contact,s.number
     from  from list c, senior s
     inner join
     (
        select MIN(c.dob) minDob
               ,c.id
        from list c
        where c.id = s.id AND c.name = 'Abg' AND c.state ='qw'
        group by c.id 
     ) sq
     on c.dob = sq.minDob
        and c.id = sq.id