首页 > 解决方案 > 带有文本值的 SQL 插入查询

问题描述

有没有办法可以将它插入数据库?

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”之后,有一个字符串“房间 ID”加上选定的“房间 ID”,但是当我执行这个时,只有插入在桌子上的房间 ID 有办法我可以把字符串文本加上选择查询

结果在这里

标签: databasesqlite

解决方案


使用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;

推荐阅读