LoginSignup
5
2

More than 5 years have passed since last update.

[MySQL]サブクエリの結果を別テーブルにinsertする時に、ハマったこと

Last updated at Posted at 2016-05-08

サブクエリで取得した結果を、別のテーブルに挿入するときにハマったこと。

Qiitaに投稿するには稚拙すぎるかもと思いましたが、自分のためにも投稿して記録しておきます・・・。

ハマったこと

weathers テーブルの weather カラムに、 days テーブルから取得した weather カラムを挿入する。

正しいクエリ文
insert into weathers (weather) select distinct weather from days;

間違ったクエリ文
insert into weathers (weather) values (select distinct weather from days);
エラーメッセージ
You have an error in your SQL syntax;
check the manual that 
corresponds to your MySQL server version 
for the right syntax to use near 
'select distinct weather from days)' at line 1

まとめ

サブクエリで取得した結果をつっこむSQL文には、Valuesはいらない。

5
2
0

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
5
2