数据库常用sql总结

这篇博客集中总结了Postgres数据库的一些常见操作,包括获取表记录数、查询主键信息以及多表JOIN的方法,并将持续更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇博客是对一些比较常见的数据库知识的汇总,并会持续更新。

Postgres

如何获取postgres所有的表里的记录条数?

SELECT 
  nspname AS schemaname,relname,reltuples
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE 
  nspname NOT IN ('pg_catalog', 'information_schema') AND
  relkind='r' 
ORDER BY reltuples DESC;

如何获取postgres里某个表的主键?

    public List<String> getPrimaryKeysByTable(String tableName, JdbcTemplate jdbcTemplate) {
        String retrievePrimaryKeySql =
                "select kc.column_name from information_schema.table_constraints tc join information_schema.key_column_usage kc on kc.table_name = \'"
                        + tableName
                        + "\' and kc.table_schema = \'public\' and kc.constraint_name = tc.constraint_name where tc.constraint_type = \'PRIMARY KEY\'  and kc.ordinal_position is not null order by column_name";
        return jdbcTemplate.queryForList(retrievePrimaryKeySql, String.class);
    }

join两个表:

SELECT * FROM billitem INNER JOIN bill ON (billitem.bill_id = bill.id and bilL.market_id = 'ROC') ;

join三个表:

select * from billitem_charges b1 inner join billitem b2 on b1.billitem_id = b2.id
inner join bill b3 on b2.bill_id =b3.id and b3.market_id = 'ROC';

例子:
 

select t.* from technicalresource t join item i  
on t.internal_id = i.internalid join "snapshot" s 
on i.snapshot_id=s.snapshotid and s.subscription_id = 'E25A2EE0-7A10-4F85-AB1E-E3E2BB770725' ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值