0

同じ形式で異なるデータが入力された 2 つの同一のテーブルがあります。

andを使用してSELECTクエリを実行したいのですが、クエリが非常に長くなるため、a を使用したくありません。group byorder byUNION

動作するサンプルを次に示します。

(SELECT a, b, c, d, e, f, g, MIN(h) as h, i 
 FROM `table1` 
 WHERE a LIKE '%this%' AND b LIKE '%that%' 
 GROUP BY b, a,c) 
UNION 
(SELECT a, b, c, d, e, f, g, MIN(h) as h, i 
 FROM `table2` 
 WHERE a LIKE '%this%' AND b LIKE '%that%' 
 GROUP BY b, a,c) 
ORDER BY 
    b DESC, h ASC, c ASC

クエリを機能させるよりエレガントな方法はありますか?

何かのようなもの

(SELECT a, b, c, d, e, f, g, MIN(h) as h, i 
 FROM `table1`,`table2` 
 WHERE a LIKE '%this%' AND b LIKE '%that%' 
 GROUP BY b, a, c) 
ORDER BY 
    b DESC, h ASC, c ASC`
4

1 に答える 1

2

あなたが欲しいのはunion allサブクエリです:

SELECT a, b, c, d, e, f, g, MIN(h) as h, i
FROM ((select table1.*
       from table1
      ) union all
      (select table2.*
       from table2
      )
     ) t
WHERE a LIKE '%this%' AND b LIKE '%that%'
GROUP BY b, a,c
order by b DESC, h ASC, c ASC

集計関数がないためe, f, g, i、クエリは単なる代表的なものだと思います。select

于 2012-12-06T15:44:35.833 に答える