1

これにより、外部資金を使用したユーザーのリストが表示されます。

SELECT
  table_user.user as user,
  sum(table_deposit.amount) as TotalExternalDeposits,payby,pro_id
FROM table_deposit inner join table_user on table_deposit.user = table_user.user 
WHERE  table_deposit.pro_id <>  'Cash A/C'
AND  table_deposit.batch NOT LIKE  '%adj%'
AND table_deposit.batch NOT LIKE  'Xmas%'
AND table_deposit.batch NOT LIKE  'X-mas%'
group by table_user.user
order by table_user.user

私の問題は、外部資金 ( TotalExternalDeposits = 0) を使用していないユーザーのリストが必要なことです。道に迷いました。

次のようなものを追加しようとするとHAVING TotalExternalDeposits = 0、空のセットが得られます。外部資金を使用していないユーザーが何千人もいることがわかっています。

4

4 に答える 4

2

joinが探しているユーザーを除外していないと仮定すると、次のように使用できnotます。

SELECT table_user.user as user, sum(table_deposit.amount) as TotalExternalDeposits,payby,pro_id
FROM table_deposit inner join
     table_user
    on table_deposit.user = table_user.user 
WHERE not (`table_deposit`.pro_id <>  'Cash A/C' AND 
           `table_deposit`.batch NOT LIKE  '%adj%' AND
           table_deposit.batch NOT LIKE  'Xmas%' AND
           table_deposit.batch NOT LIKE  'X-mas%'
          )
group by `table_user`.user
order by `table_user`.user

ただし、これにより、「外部資金」ではないアカウントを持つユーザーが取得されます。つまり、上記は少なくとも 1 つの非外部資金口座を持つユーザーを取得します。アカウントが外部からの資金提供ではないことを確認する必要がある場合があります (すべての ではありません)。その場合、条件をhaving節に移動して、一致する行を数えることができます。値が 0 であることを確認します。

SELECT tu.user as user, sum(td.amount) as TotalExternalDeposits, payby, pro_id
FROM table_user tu left outer join
     table_deposit td
     on td.user = tu.user 
group by tu.user
having sum((td.pro_id <>  'Cash A/C' AND 
            td.batch NOT LIKE  '%adj%' AND
            td.batch NOT LIKE  'Xmas%' AND
            td.batch NOT LIKE  'X-mas%'
           ) or td.user is null
          ) = 0
order by tu.user;

また、テーブルにはテーブル エイリアスを使用しました。そのほうが読みやすいと思います。

于 2013-07-27T22:21:34.383 に答える
0

どちらかまたはどちらかであり、クエリの反対のセットが必要な場合は、基準を逆にします。

SELECT
  table_user.user as user,
  sum(table_deposit.amount) as TotalExternalDeposits,payby,pro_id
FROM table_deposit inner join table_user on table_deposit.user = table_user.user 
WHERE  table_deposit.pro_id =  'Cash A/C'
OR  table_deposit.batch  LIKE  '%adj%'
OR table_deposit.batch  LIKE  'Xmas%'
OR table_deposit.batch  LIKE  'X-mas%'
group by table_user.user
order by table_user.user
于 2013-07-28T02:15:38.407 に答える
0

これでうまくいくと思いますが、句にandHAVINGを追加するのを忘れているように見えるか、エイリアスを試したために失敗した可能性があります。paybypro_idGROUP BY

 SELECT
   table_user.user as user,
   sum(table_deposit.amount) as TotalExternalDeposits,payby,table_deposit.pro_id
 FROM table_deposit inner join table_user on table_deposit.user = table_user.user 
 WHERE  table_deposit.pro_id <>  'Cash A/C'
 AND  table_deposit.batch NOT LIKE  '%adj%'
 AND table_deposit.batch NOT LIKE  'Xmas%'
 AND table_deposit.batch NOT LIKE  'X-mas%'
 GROUP BY table_user.user,payby,table_deposit.pro_id
 HAVING sum(table_deposit.amount) = 0
 ORDER BY table_user.user
于 2013-07-28T03:49:38.833 に答える