需要一个MYSQL查询

时间:2017-03-29 01:01:14

标签: mysql

我是mysql的新手, 我有一张表<!DOCTYPE html> <html> <head> <style> .popup { position: fixed; top: 0px; left: 0px; bottom:0px; right: 0px; margin: auto; width: 200px; height: 150px; font-family: verdana; font-size: 13px; padding: 10px; background-color: rgb(240, 240, 240); border: 2px solid grey; z-index: 100000000000000000; } .blur { filter: blur(5px); -webkit-filter: blur(5px); -moz-filter: blur(5px); -o-filter: blur(5px); -ms-filter: blur(5px); } .cancel { display: relative; cursor: pointer; margin: 0; float: right; height: 10px; width: 14px; padding: 0 0 5px 0; background-color: red; text-align: center; font-weight: bold; font-size: 11px; color: white; border-radius: 3px; z-index: 100000000000000000; } .cancel:hover { background: rgb(255, 50, 50); } #overlay1, #overlay2, #overlay3 { position: fixed; display: none; left: 0px; top: 0px; right: 0px; bottom: 0px; background: rgba(255, 255, 255, .8); z-index: 999; } #popup { position: absolute; width: 400px; height: 200px; background: rgb(255, 255, 255); border: 5px solid rgb(90, 90, 90); left: 0px; right: 0px; top: 0px; bottom: 0px; margin: auto; } </style> </head> <body> <div id="overlay1"> <div id="popup"> <h3>POPUP 1</h3> http://www.google.com <a href="javascript:myBlurFunction(0, 'popup1');"> hide</a></div> </div> <div id="main_container1"> <a href="javascript:myBlurFunction(1, 'popup1');">OPEN POPUP 1</a><br/> </div> <br/> <div id="overlay2"> <div id="popup"> <h3>POPUP 2</h3> http://www.cnn.com <a href="javascript:myBlurFunction(0, 'popup2');"> hide</a></div> </div> <div id="main_container2"> <a href="javascript:myBlurFunction(1, 'popup2');">OPEN POPUP 2</a><br/> </div> <br/> <div id="overlay3"> <div id="popup"> <h3>POPUP 3</h3> http://www.yahoo.com<a href="javascript:myBlurFunction(0, 'popup3');"> hide</a></div> </div> <div id="main_container3"> <a href="javascript:myBlurFunction(1, 'popup3');">OPEN POPUP 3</a><br/> </div> <script type="text/javascript"> var myBlurFunction = function(state, popup_type) { if (state == 1) { if (popup_type == "popup1") { var containerElement = document.getElementById('main_container1'); var overlayEle = document.getElementById('overlay1'); overlayEle.style.display = 'block'; containerElement.setAttribute('class', 'blur'); } else { if (popup_type == "popup2") { var containerElement = document.getElementById('main_container2'); var overlayEle = document.getElementById('overlay2'); overlayEle.style.display = 'block'; containerElement.setAttribute('class', 'blur'); } else { var containerElement = document.getElementById('main_container3'); var overlayEle = document.getElementById('overlay3'); overlayEle.style.display = 'block'; containerElement.setAttribute('class', 'blur'); }}} else { if (popup_type == "popup1") { var containerElement = document.getElementById('main_container1'); var overlayEle = document.getElementById('overlay1'); overlayEle.style.display = 'none'; containerElement.setAttribute('class', 'null');} else { if (popup_type == "popup2") { var containerElement = document.getElementById('main_container2'); var overlayEle = document.getElementById('overlay2'); overlayEle.style.display = 'none'; containerElement.setAttribute('class', 'null');} else { var containerElement = document.getElementById('main_container3'); var overlayEle = document.getElementById('overlay3'); overlayEle.style.display = 'none'; containerElement.setAttribute('class', 'null'); }} } }; </script> <Center>All good men try to do their level best.</Center> </body> </html> ,其数据显示在此图片中

enter image description here

我需要一个查询,我会传递一个user_id说 CBS_00002 ,它会返回 grand_level 对于不同包来说最高的行。

e.g。

enter image description here

请帮助我。

1 个答案:

答案 0 :(得分:1)

您当前的查询缺少两件事。首先,您应该使用GROUP BY,因为您想要找到每个包组的最大宏级别,而不是整个表。其次,您应该使用子查询来查找最大级别,然后使用它来限制您希望出现在结果集中的记录。

SELECT t1.*
FROM grand_score_master t1
INNER JOIN
(
    SELECT package_id, MAX(grand_level) AS max_grand_level
    FROM grand_score_master
    GROUP BY package_id
) t2
    ON t1.package_id  = t2.package_id AND
       t1.grand_level = t2.max_grand_level