SQL插入到表中,其中从其他表中选择的值匹配其他表
我有以下表:
table1 :
id | Rarity |
---|---|
1 | common |
2 | 罕见 |
3 | 稀有 |
table2 :
id | type |
---|---|
1 | Air |
2 Air 2 | Earth |
3 | Earth 3 Fire |
4 | 水 |
已经存在输出表和架构如下:
RarityId | feekness_typeid | ustimance_typeid |
---|
,我应该根据表2和表1填充行。
例如,如果我给出:
类型
是'water'和'air'rarity
是'common'
我想添加table1中包含的ID
和表2
到此表获取以下更新的输出表:
rarityId | feekness_typeid | ustistance_typeid |
---|---|---|
1 | 4 | 1 |
我写了以下查询:
INSERT INTO table3 (rarityID, weakness_typeID, resistance_typeID)
SELECT rar.id, weak.id, res.id
FROM table1 rar, table2 weak, table2 res
WHERE rar.rarity = `Common`
AND weak.type = `Water`
AND res.type = `Air`;
但是它不起作用,您可以帮助我吗?
I have the following tables:
Table1:
id | rarity |
---|---|
1 | Common |
2 | Uncommon |
3 | Rare |
Table2:
id | Type |
---|---|
1 | Air |
2 | Earth |
3 | Fire |
4 | Water |
The output table already exists and the schema is the following:
rarityID | weakness_typeID | resistance_typeID |
---|
and I should fill it with rows according to the Table2 and Table1.
For example if I'm given:
type
is 'Water' and 'Air'rarity
is 'Common'
I'd like to add the IDs contained in Table1
and Table2
to this table to get the following updated output table:
rarityID | weakness_typeID | resistance_typeID |
---|---|---|
1 | 4 | 1 |
I've written the following query:
INSERT INTO table3 (rarityID, weakness_typeID, resistance_typeID)
SELECT rar.id, weak.id, res.id
FROM table1 rar, table2 weak, table2 res
WHERE rar.rarity = `Common`
AND weak.type = `Water`
AND res.type = `Air`;
But it doesn't work, can you help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对您的问题的理解是,您正在尝试获取每个信息的ID。
如果这是正确的,为了执行此操作,您需要在以下查询中完成三个单独的查询中选择其ID:
如果要使用此代码播放,请检查此 sql小提琴。
My understanding of your problem is that you're trying to get ids for each of your information.
If this is correct, in order to do this you need to select their ids in three separate queries like it is done in the following query:
If you want to play with this code, check this SQL Fiddle.