SQL插入查询与文本值

时间:2019-03-02 14:33:23

标签: database sqlite

有没有一种方法可以将其插入数据库?

INSERT INTO Bill_Table (Patient_ID, [Med/Room], Bill, [Paid/UnPaid])
VALUES (13, 'Room ID' + (SELECT Room_ID 
                         FROM Room_Master 
                         WHERE [Room_No.] = 2),
        (SELECT Price  
         FROM Room_Type 
         WHERE Room_Type = 'Semi-Private'), 'UnPaid');

在值上的'13'之后有一个字符串“ Room ID”加上所选的“ Room ID”,但是当我执行此操作时,只有插入在表上的Room ID可以将字符串文本加选择查询

The result here

2 个答案:

答案 0 :(得分:0)

使用insert . . . select

insert Into Bill_Table (Patient_ID, [Med/Room], Bill,[Paid/UnPaid])
    Select 13, concat('Room ID', rm.Room_ID), rt.price, 'UnPaid'
    from Room_Master rm left join
         Room_Type rt
         on rt.Room_Type = 'Semi-Private'
    where rm.[Room_No.] = 2;

在SQLite中,您将使用concat运算符:

Insert Into Bill_Table (Patient_ID, [Med/Room], Bill,[Paid/UnPaid])
    Select 13, 'Room ID' || rm.Room_ID, rt.price, 'UnPaid'
    from Room_Master rm left join
         Room_Type rt
         on rt.Room_Type = 'Semi-Private'
    where rm.[Room_No.] = 2;

答案 1 :(得分:0)

您的代码唯一的问题是使用+进行串联,这在SQLite中不起作用。
而是使用||运算符:

Insert Into Bill_Table (Patient_ID,[Med/Room],Bill,[Paid/UnPaid])
  values (
    13,
    'Room ID' || ' ' || (Select Room_ID from Room_Master where [Room_No.] = 2),
    (Select Price from Room_Type where Room_Type = 'Semi-Private'),
    'UnPaid'
  );