-1

MySQL データベース用の SQL の生成についてサポートが必要です。

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

  • 組織
  • メンバー
  • 支払い

組織表:

+------------+---------+--------+
|    id      |  name   |website |
+------------+---------+--------+
| 1          | AAA     | a.com  |
|-------------------------------+
| 2          | BBB     | b.com  |
+------------+---------+--------+

メンバー表:

+------------+-------------------+--------+-----------------+-----------+
|    id      |  organisation_id  |name    | Payment_confirm | join_date |
+------------+-------------------+--------+-----------------+-----------+
| 1          | 1                 | james  | 1               | 2013-8-02 |
|-----------------------------------------+-----------------+-----------+
| 2          | 1                 | Jimmy  | 0               | 2013-6-25 |
+------------+-------------------+--------+-----------------+-----------+
| 3          | 2                 | Manny  | 1               | 2013-07-02|
|-----------------------------------------+-----------------+-----------+
| 4          | 1                 | Kim    | 1               | 2013-09-02|
+------------+-------------------+--------+-----------------+-----------+

支払い表:

+------------+-------------------+--------+-----------------+----------------+
|    id      |  member_id        |amount  | transaction_id  | transferred_at |
+------------+-------------------+--------+-----------------+----------------+
| 1          | 1                 | 100    | T1001           | 2013-8-03      |
|-----------------------------------------+-----------------+--------------- +
| 2          | 2                 | 0      | null            | Null           |
+------------+-------------------+--------+-----------------+----------------+
| 3          | 3                 | 200    | T1002           | Null           |
|-----------------------------------------+-----------------+----------------+
| 4          | 4                 | 50     | T1005           | 2013-09-05     |
+------------+-------------------+--------+-----------------+----------------+

以下を選択するにはどうすればよいですか?

次の出力が期待されます。

+------------+-------------------+--------+-----------------+---------------+--------------+ 
|  Org name  |  Revenue          |untransferred amount      | Total members | last 30 days |
+------------+-------------------+--------------------------+---------------+--------------+
| AAA        | 150               | 0                        | 3             |  2           |
|-----------------------------------------------------------+---------------+--------------+
| BBB        | 200               | 200                      | 1             |  0           |   
+------------+-------------------+--------------------------+---------------+--------------+


Org name              = organisation name
Revenue               = Total amount received
untransferred amount  = transferred_at is null (payments table)
Total members         = total members joined till today
last 30 days          = total members joined last 30 days 
4

2 に答える 2

4

テーブルを結合し、結果をグループ化し、目的のロジックを選択する必要があります。

SELECT   org.name,
         SUM(pmt.amount) AS revenue,
         SUM(IF(pmt.transferred_at IS NULL, pmt.amount, 0)) AS untransferred
FROM     Organisations org
    JOIN Members       mem ON mem.organisation_id = org.id
    JOIN Payments      pmt ON pmt.member_id       = mem.id
GROUP BY org.id

sqlfiddleで参照してください。

于 2013-09-19T07:40:27.737 に答える