Home > OS >  how to select the last inserted row in a table
how to select the last inserted row in a table

Time:02-04

as shown below, i have a large db-table with several columns. i want to be able to select the last inserted row in that table..how can i achieve that in postgresql

output

"timeofinsertion","selectedsiteid","devenv","threshold","ec50ewco","dose","apprateofproduct","concentrationofactingr","unclass_intr_nzccs","unclass_inbu_nzccs","vl_intr_nzccs","percentage_vl_per_total_nzccs_intr","vl_inbu_nzccs","percentage_vl_per_total_nzccs_inbu","totalvlnzccsinsite","percentage_total_vlnzccs_per_site","l_intr_nzccs","percentage_l_per_total_nzccs_intr","l_inbu_nzccs","percentage_l_per_total_nzccs_inbu","totallnzccsinsite","percentage_total_lnzccs_per_site","m_intr_nzccs","percentage_m_per_total_nzccs_intr","m_inbu_nzccs","percentage_m_per_total_nzccs_inbu","totalmnzccsinsite","percentage_total_mnzccs_per_site","h_intr_nzccs","percentage_h_per_total_nzccs_intr","h_inbu_nzccs","percentage_h_per_total_nzccs_inbu","totalhnzccsinsite","percentage_total_hnzccs_per_site","unclass_intr_zccs","unclass_inbu_zccs","vl_intr_zccs","percentage_vl_per_total_zccs_intr","vl_inbu_zccs","percentage_vl_per_total_zccs_inbu","totalvlzccsinsite","percentage_total_vlzccs_per_site","l_intr_zccs","percentage_l_per_total_zccs_intr","l_inbu_zccs","percentage_l_per_total_zccs_inbu","totallzccsinsite","percentage_total_lzccs_per_site","m_intr_zccs","percentage_m_per_total_zccs_intr","m_inbu_zccs","percentage_m_per_total_zccs_inbu","totalmlzccsinsite","percentage_total_mzccs_per_site","h_intr_zccs","percentage_h_per_total_zccs_intr","h_inbu_zccs","percentage_h_per_total_zccs_inbu","totalhzccsinsite","percentage_total_hzccs_per_site","totalunclassnzccs","totalunclasszccs","totalnzccsintr","totalnzccsinbu","totalnzccsinsite","totalzccsintr","totalzccsinbu","totalzccsinsite","totalvlinsite","percentageof_total_vl_insite_per_site","totallinsite","percentageof_total_l_insite_per_site","totalminsite","percentageof_total_m_insite_per_site","totalhinsite","percentageof_total_h_insite_per_site","total_unclass_with_nodatacells_excluded","total_unclass_with_nodatacells_included","total_with_nodatacells_excluded","total_with_nodatacells_included"
"3-2-2023 10:0:3:745762","202311011423",test,1,"3.125","0.75","75","100","0","0","0","0","0","0","0","0.0","0","0","0","0","0","0.0","0","0","0","0","0","0.0","0","0","0","0","0","0.0","0","0","0","0.0","32","91.4","32","82.1","0","0.0","3","8.6","3","7.7","4","100.0","0","0.0","4","10.3","0","0.0","0","0.0","0","0.0","0","0","0","0","0","4","35","39","32","82.1","3","7.7","4","10.3","0","0.0","0","0","39","39"

CodePudding user response:

If you have a column to sort by, you can use:

select *
from the_table
order by timeofinsertion desc
limit 1;

If you want to get the complete row that you have just inserted, it might be easier to use the returning clause with your INSERT statement:

insert into the_table (timeofinsertion, selectedsiteid, ...)
values (current_timestamp, ....)
returning *;

CodePudding user response:

In MSSQL server you can you SCOPE_IDENTITY it return last inserted Identity Column Value.

Otherwise you can you order by desc with top 1 of Identity column

Check Example :

Create Table tbl_Name
(
    RowId       Int Not Null Primary Key Identity(1,1),
    Name        Varchar(100)
)

INsert Into tbl_Name (Name) Values ('My Name')

Select * From tbl_Name Where RowId = SCOPE_IDENTITY()

Select Top 1 * From tbl_Name Order by RowId desc
  • Related