2016-11-04 3 views
1

Je suis en utilisant 'sous' dans ce codealias de table MySQL ne fonctionne pas

select PERSONAL_ID,NAME,SURNAME,BIRTH_DATE,NUMBER_OF_ORDERS 
from(
select PERSONAL_ID,NAME,SURNAME,BIRTH_DATE,count(CUSTOMER_ID) as NUMBER_OF_ORDERS 
from customer as C 
right join 
customer_hotel as CH on CH.CUSTOMER_ID = C.ID 
group by(CH.CUSTOMER_ID) 
) as O 
where NUMBER_OF_ORDERS = (select max(NUMBER_OF_ORDERS) from O); 

et il dit: Table 'company.o' n'existe pas. mais quand je le fais comme ça

select PERSONAL_ID,NAME,SURNAME,BIRTH_DATE,NUMBER_OF_ORDERS 
from(
select PERSONAL_ID,NAME,SURNAME,BIRTH_DATE,count(CUSTOMER_ID) as NUMBER_OF_ORDERS 
from customer as C 
right join 
customer_hotel as CH on CH.CUSTOMER_ID = C.ID 
group by(CH.CUSTOMER_ID) 
) as O 
where NUMBER_OF_ORDERS = (select max(NUMBER_OF_ORDERS) 
from(
select PERSONAL_ID,NAME,SURNAME,BIRTH_DATE,count(CUSTOMER_ID) as NUMBER_OF_ORDERS 
from customer as C 
right join 
customer_hotel as CH on CH.CUSTOMER_ID = C.ID 
group by(CH.CUSTOMER_ID) 
) as O); 

cela fonctionne correctement. Aucune suggestion?

+0

Désolé, mais je pense que tout cela est un peu un gâchis (y compris le bit que vous pensez « fonctionne correctement »). Voir http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry

+0

Qu'essayez-vous d'accomplir? – FJT

+0

@CGritton oui. c'est –

Répondre

1

L'attribution d'un alias à une sous-requête ne signifie pas qu'elle peut être traitée exactement comme une table.

Dans la première requête, mysql n'a aucun moyen de savoir que le O dans select max(NUMBER_OF_ORDERS) from O n'est pas une table. C'est comme ça que ça se passe, d'où l'erreur. Le nom de votre schéma est company?

BTW, peut-être vous pourriez revenir aux mêmes informations:

select PERSONAL_ID,NAME,SURNAME,BIRTH_DATE,count(CUSTOMER_ID) as NUMBER_OF_ORDERS 
from customer as C 
right join 
customer_hotel as CH on CH.CUSTOMER_ID = C.ID 
group by(CH.CUSTOMER_ID) 
order by count(CUSTOMER_ID) desc 
limit 1;