0

Is there a way that i can Insert this to database?

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');

after the ' 13 ' on the values there is a string "Room ID" plus the selected "Room ID" but when I execute this only the Room ID inserted on the table is there a way I can put string text plus the select query

The result here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

Use 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;

In SQLite, you would use the concat operator:

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;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Sorry sir but there is no concat function on sqlite but Thank you sir btw – Dummy Dummy Mar 02 '19 at 14:52
  • @DummyDummy . . . That is why there is a second answer for SQLite. Your original question was not clear on the database you are using -- in fact, it was I who removed the MySQL tag. – Gordon Linoff Mar 02 '19 at 16:27
0

The only problem with your code is the use of + for cocatenation, which does not work in SQLite.
Instead use the || operator:

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'
  );
forpas
  • 160,666
  • 10
  • 38
  • 76