Heim  >  Fragen und Antworten  >  Hauptteil

Java – Datenbankabfrage mehrerer Tabellen

Voraussetzung mündlich, mybatis
Jetzt gibt es mehrere Tabellen
Ich muss jetzt die Informationsmenge in jeder Tabelle zählen, also count(*)

Meine aktuelle Methode besteht darin, mehrere Methoden zu schreiben
Zum Beispiel im Mapper:
long selectCountA;
long selectCountB;
long selectCountC;

In diesem Fall muss ich die Datenbank dreimal überprüfen. Holen Sie sich jeweils 3 Daten. Ich frage mich, ob ich eine SQL-Anweisung schreiben kann, um drei Werte direkt zu erhalten
Brauchen Sie eine Lösung?

. Können Sie mir eine Oracle-Erklärung geben? Es ist alles MySQL. .

習慣沉默習慣沉默2712 Tage vor775

Antworte allen(4)Ich werde antworten

  • 巴扎黑

    巴扎黑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;

    Antwort
    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

    这样?

    Antwort
    0
  • 过去多啦不再A梦

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

    Mysql

    Oracle
    在以上语句后面加 from dual

    Antwort
    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也有类似的系统表

    Antwort
    0
  • StornierenAntwort