1

How can I insert a data from one table to another table according to a column condition For example one table there is a column.if its value is 1 then insert Database:Firebird 2.5

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    What did you try? Show us, please. Example Data and expected results will be appreciated as well. – Useme Alehosaini Dec 13 '20 at 01:30
  • There is a table named CARI, and there is a column named EFATURA_KULLAN,I want to insert another table if EFATURA_KULLAN=1.there are 10.000 lines in CARI table and only 3000 lines EFATURA_KULLAN=1. – Birikim Bilgisayar Dec 13 '20 at 06:43
  • `insert into some_table (....) select ... from another_table where "some condition"` –  Dec 13 '20 at 10:24
  • Please **[edit]** your question (by clicking on the [edit] link below it) and add some sample data and the expected output based on that data as [formatted text](https://meta.stackoverflow.com/a/251362). See [here](https://meta.stackexchange.com/questions/81852) for some tips on how to create nice looking text tables. ([edit] your question - please do **not** put code or additional information in comments) –  Dec 13 '20 at 10:25
  • @Birikim Bilgisayar Based on the example you provided in your comment, my answer post as below. Hope it worked with you? – Useme Alehosaini Dec 13 '20 at 11:41

1 Answers1

2

Based on the example you provided in your comment:

INSERT INTO some_table (CARI_id, anydatacomefromcari ) 
SELECT EFATURA_KULLAN, someotherdata FROM CARI
WHERE EFATURA_KULLAN = 1

fiddle example

Results:

The Script used for test is:

CREATE TABLE CARI (id INTEGER, EFATURA_KULLAN INTEGER, someotherdata INTEGER);

INSERT INTO CARI VALUES (1, 1, 55);
INSERT INTO CARI VALUES (2, 1, 44);
INSERT INTO CARI VALUES (3, 0, 99);
INSERT INTO CARI VALUES (4, 1, 12);

SELECT * FROM CARI;

CREATE TABLE some_table (CARI_id INTEGER, anydatacomefromcari INTEGER );

INSERT INTO some_table (CARI_id, anydatacomefromcari ) 
SELECT EFATURA_KULLAN, someotherdata FROM CARI
WHERE EFATURA_KULLAN = 1

SELECT * FROM some_table

Results:

+---------+-------------------------+
| CARI_id | anydatacomefromcari     |
+---------+-------------------------+
| 1       | 55                      |
+---------+-------------------------+
| 1       | 44                      |
+---------+-------------------------+
| 1       | 12                      |
+---------+-------------------------+
Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
  • I have solved my question. Thanks everyone.My used structure. EXECUTE BLOCK AS BEGIN FOR SELECT DO BEGIN END. – Birikim Bilgisayar Dec 13 '20 at 14:35
  • I am sorry I dont know how use forums – Birikim Bilgisayar Dec 13 '20 at 14:38
  • @Birikim Bilgisayar No worries, if you found the answer is beneficial mark it as accepted, how to do that see https://stackoverflow.com/tour for this and for general usage, it will be beneficial for you in the future – Useme Alehosaini Dec 13 '20 at 14:42
  • 1
    @BirikimBilgisayar declarative style insert-from-select is better (more concise and more idiomatic, and less prone to typos) than imperative style PSQL script. Unless there are hard reasons for PSQL micro-management, single INSERT would be better solution – Arioch 'The Dec 13 '20 at 17:31