Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

Problem with UNION result in Yii2 Framework

$select = (new yiidbQuery())
            ->select('autori.IDAutore, autori.IParte, autori.IIParte, autori.Prefisso, autori.Qualificazione, count(autori.IDAutore) AS qta')
            ->from('autori')->where('1=1')
            ->innerJoin('bibliografie b', '
            b.RIDAutorePrinc1 = autori.IDAutore 
        ')->limit(10)->orderby('COUNT(*) DESC');
        
        $select1 = (new yiidbQuery())
            ->select('autori.IDAutore, autori.IParte, autori.IIParte, autori.Prefisso, autori.Qualificazione, count(autori.IDAutore) AS qta')
            ->from('autori')->where('1=1')
            ->innerJoin('bibliografie b', '
            b.RIDAutorePrinc2 = autori.IDAutore
        ')->limit(10)->orderby('COUNT(*) DESC');
      
$select->union($select1);

$autoreList = $select->groupby('autori.IDAutore')->limit(10)->all();

This will return:

Michael (10)
Michael(4)

I want: Michael (14)

Which is the problem? Thank you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

union return sperated rows .. so you have two values coul be you need another join condition of a single query instead of a union between two queries

$select = (new yiidbQuery())
            ->select('autori.IDAutore
                , autori.IParte
                , autori.IIParte
                , autori.Prefisso
                , autori.Qualificazione
                , count(autori.IDAutore) AS qta')
            ->from('autori')->where('1=1')
            ->innerJoin('bibliografie b', '
            b.RIDAutorePrinc1 = autori.IDAutore OR b.RIDAutorePrinc2 = autori.IDAutore
        ')->limit(10)->orderby('COUNT(*) DESC');

if you have performance problem for a complex query you could try using pure sql and a sql command eg:

$connection = Yii::$app->db;
$q = " select t.IDAutore
    , t.IParte
    , t.IIParte
    , t.Prefisso
   , t.Qualificazione
   , sum( num_autori)
  from  (


select autori.IDAutore
    , autori.IParte
    , autori.IIParte
    , autori.Prefisso
   , autori.Qualificazione
   , count(autori.IDAutore)  num_autori
  from autori 
  inner join bibliografie b on  b.RIDAutorePrinc1 = autori.IDAutore  
  order by num_autori 
  limit 10 
  union 
select autori.IDAutore
    , autori.IParte
    , autori.IIParte
    , autori.Prefisso
   , autori.Qualificazione
   , count(autori.IDAutore)  num_autori
  from autori 
  inner join bibliografie b on  b.RIDAutorePrinc2 = autori.IDAutore  
  order by num_autori 
  limit 10 
 union 
  ....
  .....
  ....
select autori.IDAutore
    , autori.IParte
    , autori.IIParte
    , autori.Prefisso
   , autori.Qualificazione
   , count(autori.IDAutore)  num_autori
  from autori 
  inner join bibliografie b on  b.RIDAutorePrinc2 = autori.IDAutore  
  order by num_autori 
  limit 10 ) t 
 group by t.IDAutore
    , t.IParte
    , t.IIParte
    , t.Prefisso
   , t.Qualificazione"

$autoreListArray = $connection->createCommand($q)->queryAll();

$autoreListArray is an array with resulting list aggreagted by autore and with the sum of the single count in each select


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...