我需要一個MySQL查詢來獲得結果,其中不會有大部分和最少重複的值。MySQL查詢,將省略至少和最多次重複的結果
這是什麼意思?
如果MySQL查詢:
SELECT Ocjena,COUNT(*) FROM ocjenjivanje GROUP BY Ocjena;
回報:
我需要查詢,這將給我的下一個結果:
Ocjena
1
3
5
感謝
我需要一個MySQL查詢來獲得結果,其中不會有大部分和最少重複的值。MySQL查詢,將省略至少和最多次重複的結果
這是什麼意思?
如果MySQL查詢:
SELECT Ocjena,COUNT(*) FROM ocjenjivanje GROUP BY Ocjena;
回報:
我需要查詢,這將給我的下一個結果:
Ocjena
1
3
5
感謝
試試這個:
SELECT o.*
FROM (SELECT Ocjena, COUNT(*) AS c FROM ocjenjivanje GROUP BY Ocjena) AS o
INNER JOIN (SELECT Min(c) AS l, Max(c) AS h FROM (SELECT Ocjena, COUNT(*) AS c FROM ocjenjivanje GROUP BY Ocjena)) AS m
ON o.c <> m.l AND o.c <> m.h
----OR
--ON o.c NOT IN(m.l, m.h)
,或者您可以使用此查詢:
SELECT Ocjena, c
FROM
(SELECT o.*, @min:=IF(@min > o.c, o.c, @min) AS l, @max:=IF(@max < o.c, o.c, @max) AS h
FROM (SELECT Ocjena, COUNT(*) AS c FROM ocjenjivanje GROUP BY Ocjena) AS o
, (select @max:=0, @min:=32000) AS m) AS x
WHERE c NOT IN(l, h)
這裏是我的解決方案。
我不知道我可以更好地看起來更專業或更快,或類似的東西。
SELECT * FROM `Ocjenjivanje` WHERE Ocjena != ( SELECT Ocjena
FROM `Ocjenjivanje`
GROUP BY `Ocjena`
ORDER BY `Ocjena` DESC
LIMIT 1) AND OCjena != (SELECT Ocjena
FROM `Ocjenjivanje`
GROUP BY `Ocjena`
ORDER BY `Ocjena` ASC
LIMIT 1)
GROUP BY `Ocjena`
但這個查詢返回Ocjena {2,3,4},而不是「主要和最少重複的值」... –
你可以得到這樣的查詢張貼在你的答案(但不返回預期的問題行)使用此查詢之一:
SELECT o.*
FROM (SELECT Ocjena, COUNT(*) AS c FROM ocjenjivanje GROUP BY Ocjena) AS o
INNER JOIN (SELECT Min(Ocjena) AS l, Max(Ocjena) AS h FROM ocjenjivanje) AS m
ON o.Ocjena <> m.l AND o.Ocjena <> m.h
SELECT Ocjena, COUNT(*) AS c
FROM ocjenjivanje AS o
WHERE o.Ocjena <> (SELECT Min(Ocjena) FROM ocjenjivanje)
AND o.Ocjena <> (SELECT Max(Ocjena) FROM ocjenjivanje)
GROUP BY Ocjena
SELECT Ocjena, COUNT(*) AS c
FROM ocjenjivanje AS o
WHERE o.Ocjena NOT IN (SELECT Min(Ocjena) FROM ocjenjivanje UNION SELECT Max(Ocjena) FROM ocjenjivanje)
GROUP BY Ocjena
當然1,3,4 !!!!!!!!!!!!!!!!!! – Strawberry