首頁  >  問答  >  主體

java - 資料庫查詢多表

前提 oralce,mybatis
現在有多張表
我現在需要統計每張表裡面的資訊的數量,也就是count(*)

我現在的方法是寫了多個方法
例如 mapper裡:
long selectCountA;
long selectCountB;
long selectCountC;

這樣的話,我要去資料庫裡查三次。分別獲得3個資料
我想能不能 寫一句sql語句 直接得到三個值

求解?

。能給我一個oracle語句的嗎, 咋都是mysql。 。

習慣沉默習慣沉默2712 天前778

全部回覆(4)我來回復

  • 巴扎黑

    巴扎黑2017-05-18 10:47:06

    select "a" name, count(1)
    from tableA
    union
    select "b" name, count(1)
    from tableB
    union
    select "C" name, count(1)
    from tableC
    

    採用多列的寫法

    with 
    temp_a as (select count(*) num from talbeA),
    temp_b as (select count(*) num from tableB),
    temp_c as (select count(*) num from tableC)
    select temp_a.num, temp_b.num, temp_c.num from dual;

    回覆
    0
  • PHPz

    PHPz2017-05-18 10:47:06

    select A.countA,B.countB from (select count(*) as countA from t_countA) as A ,(select count(*) as countB from t_countB) as B

    這樣?

    回覆
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-18 10:47:06

    Mysql

    Oracle
    在以上語句後面加 from dual

    回覆
    0
  • 某草草

    某草草2017-05-18 10:47:06

    Mysql的
    select table_rows from information_schema.TABLES where table_schema in ('schema1','schema2','scheman') and table_name in ('tableName1','tableName2','tableNameN')
    相信 oralce也有類似的系統表

    回覆
    0
  • 取消回覆