我有以下代码:
SELECT e.Student_ID, s.Name, s.Surname, Result_IS, COUNT(*)
FROM Students s
LEFT JOIN Exams e
ON e.Student_ID=s.Student_ID
WHERE Result_IS='Negative'
GROUP BY e.Student_ID, s.Name, s.Surname, Result_IS
HAVING COUNT(*)=
(
SELECT MAX(COUNT(*)) FROM Exams
WHERE Student_ID=e.Student_ID
AND Result_IS='Negative'
GROUP BY e.Student_ID, s.Name, s.Surname, Result_IS
)
我遇到HAVING COUNT(*)
的问题,COUNT(*)
应选择package minesweeper;
import javax.swing.JToggleButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Mine extends javax.swing.JFrame {
final int wid = 9,hei = 9,noOfbombs = 10;
JToggleButton[][] blocks = new JToggleButton[hei][wid];
int[][] blox = new int[hei][wid];
ActionListener listen = new ActionListener() {
public void actionPerformed(ActionEvent e){
int i = 0, j = 0;
boolean found = false;
for ( i = 0 ; i < hei ; i++) {
for (j = 0 ; j < wid ; j++) {
if (e.getSource() == blocks[i][j]) {
found = true;
break;
}
}
if(found) break;
}
blocks[i][j].setText("Writing");
}
};
public Mine() {
initComponents();
for(int i = 0; i < hei ; i++)
{
for(int j = 0 ; j < wid;j++)
{
blocks[i][j] = new JToggleButton();
blocks[i][j].setSize(jPanel1.getWidth()/wid,jPanel1.getHeight()/hei);
jPanel1.add(blocks[i][j]);
blocks[i][j].setLocation(j*jPanel1.getWidth()/wid, i*jPanel1.getHeight()/hei);
blocks[i][j].addActionListener(listen);
}
}
}
private void resiz(){
for(int i = 0;i<hei;i++)
{
for(int j = 0;j<wid;j++)
{
blocks[i][j] = new JToggleButton();
blocks[i][j].setSize(jPanel1.getWidth()/wid, jPanel1.getHeight()/hei);
jPanel1.add(blocks[i][j]);
blocks[i][j].setLocation(j*jPanel1.getWidth()/wid, i*jPanel1.getHeight()/hei);
}
}
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
private void jPanel1ComponentResized(java.awt.event.ComponentEvent evt) {
resiz();
}
public static void main(String args[]) {
//@override
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Mine().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration
给出最大结果的行,而只是给我第一个选择的输出,如下所示:
我一直在尝试各种各样的事情,但没有什么可以选择行,其中count是最大值。
请给我一个提示,请从哪里开始,以及代码有什么问题。
答案 0 :(得分:1)
为什么不按count(*)desc排序并选择top 1?
SELECT top 1 e.Student_ID, s.Name, s.Surname, Result_IS, COUNT(*) FROM Students s LEFT JOIN Exams e
ON e.Student_ID=s.Student_ID
WHERE Result_IS='Negative'
GROUP BY e.Student_ID, s.Name, s.Surname, Result_IS
order by count(*) desc
我认为这是Oracle
的语法 Select * FROM
(
SELECT e.Student_ID, s.Name, s.Surname, Result_IS, COUNT(*) FROM Students s LEFT JOIN Exams e
ON e.Student_ID=s.Student_ID
WHERE Result_IS='Negative'
GROUP BY e.Student_ID, s.Name, s.Surname, Result_IS
order by count(*) desc
)
Where rownum=1
答案 1 :(得分:1)
待办事项
HAVING COUNT(*)=
(
SELECT MAX(COUNT(*)) FROM Exams
WHERE Student_ID=e.Student_ID
AND Result_IS='Negative'
GROUP BY e.Student_ID, s.Name, s.Surname, Result_IS
)
查询已连接的表。 :)
答案 2 :(得分:0)
所以,我发现了如何做到,解决方案如下:
SELECT e.Student_ID, s.Name, s.Surname, Result_IS, COUNT(*) FROM Students s LEFT JOIN Exams e
ON e.Student_ID=s.Student_ID
WHERE Result_IS='Negative'
GROUP BY e.Student_ID, s.Name, s.Surname, Result_IS
HAVING COUNT(*)=
(
SELECT MAX(count) from
(
SELECT count(*) as count FROM Exams
WHERE Result_Is='Negative'
GROUP BY Student_ID
)
)