我有一个INSERT INTO SELECT
语句,目前它有一个静态数字。我希望通过将其设置为另一个表中字段的最大值来使此数字动态。
语句如下:
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'
这是每个表格的示例:
这是我的
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"
这是我的
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
这里是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
我该如何将SELECT 3
更改为类似于SELECT MAX(checklists.id)
的内容?