-1

私はmysqlに3つのテーブルを持っています

table 1
date        name   total recieve
2013-05-09  "aa"    20    15
2013-05-09  "bb"    10    17

table 2
name    tree
"bb"     "a1"
"aa"     "a2"

table 3
date          tree      users
2013-05-09   "a1SI"      19
2013-05-09   "a1NO"      24
2013-05-09   "a2SI"      39
2013-05-09   "a2NO"      22

ツリー テーブルのビューが必要です

これは私が必要とするビューです:

date         name   tree   total    recieve    userSI    userNO  
2013-05-09   "aa"   "a2"    20        15        39         22
2013-05-09   "bb"   "a1"    10        17        19         24

私はクエリの半分を持っています

SELECT t1.`date` , t1.`name` , t2.`tree` , t1.`total` , t1.`recieve`,
FROM  `table1` t1
INNER JOIN  `table2` t2 ON t1.`name` = t2.`name` 
ORDER BY  `t1`.`date` DESC

しかし、私は3つのテーブルでどのようにビューを行うのかわかりません。他の問題はツリーのレコードです。ツリー「a1」と「a1Si」と「a2NO」があり、ビューで3つの列を関連付ける必要があるためです

4

2 に答える 2

10

table3次の which JOINSとt2.treeの左 2 文字を使用できるはずですt3.tree

SELECT t1.`date` , t1.`name` , t2.`tree` , t1.`total` , t1.`recieve`,
  max(case when right(t3.tree, 2) = 'SI' then t3.users end) usersSI,
  max(case when right(t3.tree, 2) = 'NO' then t3.users end) usersNO
FROM  `table1` t1
INNER JOIN  `table2` t2 
  ON t1.`name` = t2.`name` 
INNER JOIN `table3` t3
  on t2.tree = left(t3.tree, 2)
group by t1.`date` , t1.`name` , t2.`tree` , t1.`total` , t1.`recieve`
ORDER BY  `t1`.`date` DESC;

SQL Fiddle with Demoを参照してください。

これは、複数の JOIN を使用して行うこともできますtable3

SELECT t1.`date` , t1.`name` , t2.`tree` , t1.`total` , t1.`recieve`,
  t3SI.users usersSI,
  t3NO.users usersNO
FROM  `table1` t1
INNER JOIN  `table2` t2 
  ON t1.`name` = t2.`name` 
LEFT JOIN `table3` t3SI
  on t2.tree = left(t3SI.tree, 2)
  and right(t3SI.tree, 2) = 'SI'
LEFT JOIN `table3` t3NO
  on t2.tree = left(t3NO.tree, 2)
  and right(t3NO.tree, 2) = 'NO'
ORDER BY  `t1`.`date` DESC;

SQL Fiddle with Demoを参照してください。またはにLEFT JOIN一致するツリーが存在しない場合にを使用するように、最後の 2 つの結合を変更したことに気付くでしょう。その後、データが返されます。一致するデータがあることがわかっている場合は、INNER JOIN を使用できます。SINO

より長いツリー名がある場合は、JOIN に LIKE 句を実装できます。

SELECT t1.`date` , t1.`name` , t2.`tree` , t1.`total` , t1.`recieve`,
  t3SI.users usersSI,
  t3NO.users usersNO
FROM  `table1` t1
INNER JOIN  `table2` t2 
  ON t1.`name` = t2.`name` 
LEFT JOIN `table3` t3SI
  on t3SI.tree like concat(t2.tree, '%')
  and right(t3SI.tree, 2) = 'SI'
LEFT JOIN `table3` t3NO
  on t3NO.tree like concat(t2.tree, '%')
  and right(t3NO.tree, 2) = 'NO'
ORDER BY  `t1`.`date` DESC;

デモで SQL Fiddle を参照してください

于 2013-05-21T20:05:48.170 に答える
0

以下のように、3 番目のテーブルとの内部結合を 2 回試行します。

    SELECT t1.'date' , t1.'name' , t2.'tree' , t1.'total' , t1.'recieve',
      t31.users usersSI,
      t32.users usersNO
    FROM  'table1' t1
    INNER JOIN  'table2' t2 
      ON t1.'name' = t2.'name' 
    INNER JOIN 'table3' t31
      on t2.tree = left(t31.tree, 2) AND right(t31.tree, 2) = 'SI'
    INNER JOIN 'table3' t32
      on t2.tree = left(t32.tree, 2) AND right(t32.tree, 2) = 'NO'
    group by t1.'date' , t1.'name' , t2.'tree' , t1.'total' , t1.'recieve'
    ORDER BY  't1'.'date' DESC;
于 2013-05-21T20:28:31.033 に答える