0

単純なテーブル ユーザー (名前、都市、国) があり、一部の値が同じ (都市、国) の行をいくつか追加する必要があります。横にデータを挿入するより良い方法はありますか:

insert into Users (name, city, country) values 
("John", "Paris", "France"), 
("Anna", "Paris", "France"), 
("Peter", "Paris", "France"), 
("Mary", "Paris", "France")

ありがとうございました

4

4 に答える 4

2

次のようなクエリを使用できます。

insert into Users (name, city, country) 
select name, city, country
from (select 'John' as name union all 
      select 'Anna' union all 
      select 'Peter' union all 
      select 'Mary') as t1
cross join (select 'Paris' as city, 'France' as country) as t2
于 2016-07-15T14:34:29.830 に答える
0
Your solution is correct normaly but try to replace " by '   else try it:

insert into Users (name, city, country) 
select *
from (
select 'John', 'Paris', 'France' union all 
select 'Anna', 'Paris', 'France' union all 
select 'Peter', 'Paris', 'France' union all  
select 'Mary', 'Paris', 'France'
) tmp
于 2016-07-15T16:56:04.373 に答える
0

変数を使用できるはずです。だから、それは次のようになります...

set @city="Paris";

set @country="France";

insert into Users(name, city, country) values
("John", @city, @country)
于 2016-07-15T14:35:39.713 に答える
0

特定の RDBMS 構文で変数を使用することでこれをいくらか単純化できますが、テーブルを定義するときに行った設計上の決定により、同じ値をテーブルの複数の行に挿入することになりUsersます。

問題は、テーブルが正規化されていないことです。つまり、同じ情報が複数回存在します。これを修正する適切な方法は、国のテーブルを定義し、それを参照する都市のテーブルを定義し、テーブルをUsers参照に変更することですCities

于 2016-07-15T14:35:48.913 に答える