我需要用下面的子查詢幫助MySQL的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
(a.multipler):1.200,1.000
一些示例數據在這裏非常有幫助。例如,我看不到表b是如何進入查詢的 - 你沒有返回它,並且你沒有對它進行操作;爲什麼加入它?錯字? – AllInOne
添加了有問題的示例。它也應該是COUNT(b.id)... – user1432032