What is the error “Every derived table must have its own alias” in MySQL?

后端 未结 4 1598
小鲜肉
小鲜肉 2020-11-21 09:07

I am running this query on MySQL

SELECT ID FROM (
    SELECT ID, msisdn
    FROM (
        SELECT * FROM TT2
    )
);

and it is giving this

4条回答
  •  遇见更好的自我
    2020-11-21 09:50

    Here's a different example that can't be rewritten without aliases ( can't GROUP BY DISTINCT).

    Imagine a table called purchases that records purchases made by customers at stores, i.e. it's a many to many table and the software needs to know which customers have made purchases at more than one store:

    SELECT DISTINCT customer_id, SUM(1)
      FROM ( SELECT DISTINCT customer_id, store_id FROM purchases)
      GROUP BY customer_id HAVING 1 < SUM(1);
    

    ..will break with the error Every derived table must have its own alias. To fix:

    SELECT DISTINCT customer_id, SUM(1)
      FROM ( SELECT DISTINCT customer_id, store_id FROM purchases) AS custom
      GROUP BY customer_id HAVING 1 < SUM(1);
    

    ( Note the AS custom alias).

提交回复
热议问题