Search code examples
mysqlsqlmysql-error-1054

ERROR 1248 (42000): Every derived table must have its own alias , alias are present (inner query with group and having)


I need to select countries where unofficial lang-s twice more than official,+ officials are 2+

MYSQL Query:

SELECT c2.countrycode , sum(c2.isOfficial) as isFalse
FROM countrylanguage as c2
 INNER JOIN (
        select c.countrycode , sum(c.isOfficial)as isOffTrue 
        from countrylanguage as c 
        where c.isOfficial='T' 
        group by c.countrycode 
        having sum(c.isOfficial)>1)
         ) as cisT 
 ON cisT.countrycode = c2.countrycode 
  where c2.isOfficial='F' 
  group by c2.countrycode 
  having sum(c2.isOfficial)>cisT.isOffTrue*2

But I getting alias error, and can't define the root cause of the problem, could you pelase help me ?

LATER....

RC: extra ) in join

NExt Error: does not recognize internal sum() alias, could you pelase help ?

  SELECT c2.countrycode , sum(c2.isOfficial) as isFalse
FROM countrylanguage as c2
 INNER JOIN (
        select c.countrycode , sum(c.isOfficial) isOffTrue 
        from countrylanguage as c 
        where c.isOfficial='T' 
        group by c.countrycode 
        having sum(c.isOfficial)>1
         ) as cisT 
 ON cisT.countrycode = c2.countrycode 
  where c2.isOfficial='F' 
  group by c2.countrycode 
  having sum(c2.isOfficial)>(cisT.isOffTrue*2);

ERROR 1054 (42S22): Unknown column 'cisT.isOffTrue' in 'having clause'

Addition:

Table:

 +-------------+---------------+------+-----+--------+
| Field       | Type          | Null | Key | Default |
+-------------+---------------+------+-----+---------+
| CountryCode | char(3)       | NO   | PRI |         |
| Language    | char(30)      | NO   | PRI |         |
| IsOfficial  | enum('T','F') | NO   |     | F       |
+-------------+---------------+------+-----+---------+

I changed query like next one and it worked, buy I still do not get the RC of previous failure

SELECT c2.countrycode, sum(c2.isOfficial) as isOffFalse, c1.isOffTrue 
  FROM (
select c0.countrycode, sum(c0.isOfficial)as isOffTrue 
from countrylanguage c0 
where c0.isOfficial='T' 
group by c0.countrycode having sum(isOfficial)>1
) as c1, countrylanguage as c2
  where c1.countrycode = c2.countrycode 
and c2.isOfficial='F' 
group by c2.countrycode 
having sum(c2.isOfficial)>(c1.isOffTrue*2);

Solution

  • You are joining

    ON cisT.countrycode = c2.countrycode

    but countrycode column does not exist in cisT table.