我有以下表格:
表 1:
| ID | 稀有 |
|---|---|
| 1 | 常見的 |
| 2 | 罕見 |
| 3 | 稀有的 |
表2:
| ID | 型別 |
|---|---|
| 1 | 空氣 |
| 2 | 地球 |
| 3 | 火 |
| 4 | 水 |
輸出表已經存在,架構如下:
| 稀有ID | 弱點型別ID | 電阻型別ID |
|---|
我應該根據表 2 和表 1 用行填充它。
例如,如果給我:
type是“水”和“空氣”rarity常見'
我想將包含在此表中的 ID 添加到此表中Table1,Table2以獲取以下更新的輸出表:
| 稀有ID | 弱點型別ID | 電阻型別ID |
|---|---|---|
| 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`;
但它不起作用,你能幫幫我嗎?
uj5u.com熱心網友回復:
我對您的問題的理解是,您正在嘗試獲取每個資訊的 ID。
如果這是正確的,為了做到這一點,您需要在三個單獨的查詢中選擇它們的 ID,就像在以下查詢中所做的那樣:
INSERT INTO table3 (rarityID, weakness_typeID, resistance_typeID)
SELECT (SELECT rar.id
FROM table1 rar
WHERE rar.rarity = 'Common') AS rarityID,
(SELECT weak.id
FROM table2 weak
WHERE weak.type = 'Water') AS weakness_typeID,
(SELECT weak.id
FROM table2 weak
WHERE weak.type = 'Air') AS resistance_typeID;
如果您想使用此代碼,請查看此SQL Fiddle。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/475240.html
上一篇:每天計算時間范圍
