60
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MySQL — サブクエリに AS を付けないとエラーを起こす | Every derived table must have its own alias

Last updated at Posted at 2016-04-25

問題

たとえば、こんな風にサブクエリを書いたら怒られる。

SELECT *
  FROM (
    SELECT User from user
  );
=> ERROR 1248 (42000): Every derived table must have its own alias

解決

AS で何かテーブル名(エイリアス名)をつけてあげること。

SELECT *
  FROM (
    SELECT User from user
  ) AS account;
+-----------+
| User      |
+-----------+
| mysql.sys |
| root      |
+-----------+

理由

AS を書かないと、SQLの構文的に「FROM (テーブル名なし)」みたいに解釈される。

環境

  • mysql Ver 14.14 Distrib 5.7.12, for osx10.11 (x86_64) using EditLine wrapper

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

メンター受付

60
36
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
60
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?