我有一个INSERT INTO SELECT声明,现在有一个静态数字.我想通过将它作为MAX另一个表中的字段来使这个数字变得动态.
声明是:
INSERT INTO checklist_items (checklists_id, checklist_item_types_id, is_completed)
SELECT 3, cit.id, false
FROM checklist_item_types cit
WHERE cit.is_active = 't'
Run Code Online (Sandbox Code Playgroud)
以下是每个表的示例:
这是我的checklists表:
id checklist_date notes
1 "2018-07-23" "Fixed extra stuff"
2 "2018-07-24" "These are some extra notes"
3 "2018-07-25" "Notes notes"
Run Code Online (Sandbox Code Playgroud)
这是我的checklist_items表,数据减少:
id checklists_id checklists_item_types_id is_completed
1 1 1 false
2 1 2 true
3 1 3 true
...
34 2 16 true
35 2 17 true
36 2 18 true
Run Code Online (Sandbox Code Playgroud)
这是checklist_item_types,数据减少:
id description is_active
1 "Unlock Entrances" true
2 "Ladies Locker Room Lights" true
3 "Check Hot Tubs (AM)" true
...
15 "Water Softener Boiler Room" false
16 "Water Softener Laundry" true
17 "Check/Stock Fire Logs" true
18 "Drain Steam Lines (4 locations)" true
Run Code Online (Sandbox Code Playgroud)
我该如何去改变SELECT 3的东西一样SELECT MAX(checklists.id)?
只需更换3一个subquery:
INSERT INTO checklist_items (checklists_id, checklist_item_types_id, is_completed)
SELECT (SELECT MAX(id) FROM checklists), cit.id, false
FROM checklist_item_types cit
WHERE cit.is_active = 't'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
588 次 |
| 最近记录: |