好吧所以标题有点乱,但我有一张city
这样的表:
id | name | population
1 | Bla bla | 1323
2 | Blaer | 35365
现在我从另一张桌子中选择一张桌子:
INSERT INTO tmp (
name, age, city_id
)
SELECT
NAME,
AGE,
CITY
FROM main;
问题是SELECT CITY
,因为CITY
是城市的名称,但我需要id
。所以基本上我想要这样的东西,但这不起作用:
INSERT INTO tmp (
name, age, city_id
)
SELECT
NAME,
AGE,
city.id FROM city WHERE city.name = CITY
FROM main;
答案 0 :(得分:0)
使用city表执行INSERT INTO tmp (
name, age, city_id
)
SELECT
NAME,
AGE,
c.id
FROM main m
INNER JOIN city c on m.city=c.name ;
并在select子句中使用它。
adapter.getCount() == 0
答案 1 :(得分:0)
从价值中找到id有点不寻常。为什么要在主表中存储城市?你应该在那里存储城市ID。
但是简单的JOIN会做:</ p>
INSERT INTO tmp (name, age, city_id)
SELECT
m.NAME,
m.AGE,
c.id
FROM main m join city c on m.city = c.name
答案 2 :(得分:0)
试试这个
INSERT INTO tmp(
name,
age,
city_id
)
SELECT
m.name AS name,
m.age AS age,
(SELECT c.id FROM city c WHERE c.name = m.city) AS city_id
FROM main m