- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我们有一个定义了的表:
如果不存在信号,则创建表(严格的整数主键自动递增,名称为TEXT)
该表最初是空的。
我想通过选择获得给定名称的名字,如果名字不存在,则添加它并获得新的自动递增的ID。
我想使用此查询以便在需要时自动生成用作另一个表中的外键的新ID。我必须注意表演,所以我不能继续做:
检查名称是否存在,并使用SELECT返回ID
如果返回的id为null,则使用INSERT创建一个新条目
使用新的SELECT再次获得新的ID
是否可以通过单个类似SELECT的查询来完成?
谢谢!
最佳答案
我认为只有一个选择号。
假设我要在大表“崩溃”中插入id_build = 3,hashed_value = 1。
示例中的代码首先选择检查该值是否已在表中,如果是,则跳过使用where .. is null
的插入,然后从已保存到临时表中的ID中检索ID。
例如:
create temporary table if not exists Variablez(Name TEXT primary key on conflict replace, Value TEXT); --storing vars
insert into Variablez(Name, Value) values ('tmp_id', (select id_crash from crash where hashed_value = "1" )); --put id if was existing
insert into crash(id_build, hashed_value) select 3, 1 where (select Value from Variablez where Name = 'tmp_id' ) is null; -- insert if not exists
select
case
when (select Value from Variablez where name = 'tmp_id' ) is null then
'0'
else
'1'
end
as existing,
case
when (select Value from Variablez where name = 'tmp_id' ) is null then
(select id_crash from crash where hashed_value = "1")
else
(select Value from Variablez where name = 'tmp_id')
end
as id_returned;
关于sql - SQlite:如果不存在,请选择“插入”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416575/
我有两个表的一个很常见的情况:下达的“订单”和每个订单的“ItemsBought”: Orders: orderId, orderDate, [...] ItemsBought: boughtId,
我有这个问题: query 1 = SELECT emp.employeId,emp.nom,emp.prenom,msg.note,msg.quelleDate, emp.remarques, em
您好,我有一个显示枚举列表的 WPF 组合框。代码如下。 但是,当加载 View 时,它显示列表中的第一个枚举,但我希望它显示“请选择”,所以是否有 XAML 来执行此操作(如果需要, Vi
假设我们有一个定义了的表: 如果不存在信号,则创建表(严格的整数主键自动递增,名称为TEXT) 该表最初是空的。 我想通过选择获得给定名称的名字,如果名字不存在,则添加它并获得新的自动递增的ID。 我
根据选项卡的数量和屏幕方向,选项卡会彼此相邻显示或显示在下拉列表中。我写了一个函数来设置当前选项卡。这有效但不适用于下拉列表模式。必须做什么? static class TabsListener im
我有以下数据框 id pattern1 pattern2 pattern3 1 a-b-c a-b-- a-b-c 2 a-a-- a-b--
表1 id name 1 john 2 mary 3 bill 4 carrie 表2 id thing 1 ball
在 firefox 中,以下 CSS 仅应用于最后一个 div,因为它是唯一具有两个类的 div: .a.b { color: red; } Stuff A Stuff B Stuff A an
我有一个带有日期列的简单事件表。我可以轻松选择接下来的n个事件(假设n = 3): SELECT * FROM events WHERE `date` > NOW() ORDER BY `date`
我是一名优秀的程序员,十分优秀!