私は2つのテーブルを持っています:
cities(city_id|Serial, city_name|Text, state_id|Integer)
states(state_id|Serial, state_name|Text)
「PortBlair」としてcities
持つ行を挿入したいのですが、対応する行はどこから取得されますか。city_name
state_id
states
state_name="Andaman"
私の質問:
INSERT INTO cities (city_name,state_id)
VALUES ('PortBlair',select state_id from states where state_name='Andaman')
MySQLのエラー:
#1064 - 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 state_id from states where state_name='Andaman')' at line 1
質問1:このステートメントの何が問題になっていますか?いくつかのqoutes/backquotes / apostrophesがありませんか?
質問Serial
2:挿入ステートメントの順序が同じである場合、常に同じIDを生成するという意味で、データ型に依存する必要がありますか?つまり、state_id
自分自身を次のように書く必要があります。
INSERT INTO cities (city_name,state_id) VALUES ('PortBlair',1)