MySQL の次のサブクエリについてサポートが必要です
SELECT ... # Main query
...
... IN # Start of subquery that expects to return an array of a.id
(SELECT a.id, a.multipler
FROM a
JOIN b ON a.id = b.id
GROUP BY(a.id)
HAVING COUNT(b.id) * a.multipler < 5)
私が達成したいのは、テーブル b (b.id) に存在するすべての a.id をグループ化することです。結果をカウントし、a.id ごとに一意の乗算器 (a.multipler) で乗算したいと考えています。
ここでの問題は、これをサブクエリにしたいということです。したがって、2 つの結果を持つことはできません。ただし、「HAVING」を使用するには、結果セットに各変数が必要です。
結果として「a.multipler」なしで「a.id」のみが必要です。これを解決するためのアイデアはありますか?
コード例:
#table a
+------+-------------+
| a_id | a_multipler |
+------+-------------+
| 1 | 2.000 |
| 2 | 0.560 |
| 3 | 1.000 |
| 4 | 1.200 |
| 5 | 2.000 |
+----- +-------------+
#table b
+------+
| b_id |
+------+
| 1 |
| 1 |
| 1 |
| 4 |
| 4 |
| 3 |
+------+
# a.id 1: occurance in "table b": 3 x 2.000 ("a.id"==1 "a.multipler"). Fails, result >= 5
#a.id 2: Fails, no occurance of a.id in "table b".
#a.id 3: 1 x 1.000. Result < 5 OK
# and so on... "id" in "table a" (but not in "table b") is unique,
# also a "id" in "table b" have to exist in "table a".
上記のクエリ (a.id) で必要な結果: 4,3
NOT 必要な結果:
(a.id): 4,3
(倍率): 1.200、1.000