我的数据库有四个表:student
,course
,stu_course
,score
。
课程表的列: id,名称
学生表的列: id,冷杉名称,姓氏,用户名,密码
stu_course 表的列: id,stu_id,course_id (stu_id和cours_id是外键)
列得分表: id,stu_cours_id,得分,日期 (stu_cours_id是ksy的开头)
我的问题是我如何才能在分数表中获得每门课程的四个最后分数?
这是我目前拥有的:
SELECT s.first_name
, c.name
, sc.id
, k.score
, k.date
,
FROM student s
JOIN stu_course sc
ON sc.stu_id = s.id
JOIN course c
ON c.id = sc.course_id
JOIN score k
ON k.stu_course_id = sc.id
感谢您的帮助
答案 0 :(得分:1)
您可以使用ORDER BY语句对列表进行排序(使用desc的顺序对降序进行排序),然后使用LIMIT x来仅显示前x个结果。
如果您使用ORDER BY DESC LIMIT x,则按您选择的列进行排序时,您会看到最底部的x结果
答案 1 :(得分:1)
在MySQL 8.0(Maria DB> = 10.2)中,可以使用窗口函数.setIcon(...)
按降序对每门课程的分数进行排名。然后,只过滤掉排名高于4的记录:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class SpriteOnBackground extends JPanel {
// Image attribution:
// By Adam Evans - M31, the Andromeda Galaxy (now with h-alpha)
// Uploaded by NotFromUtrecht, CC BY 2.0,
// https://commons.wikimedia.org/w/index.php?curid=12654493
public static final String ANDROMEDA_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/9/98/Andromeda_Galaxy_%28with_h-alpha%29.jpg/"
+ "1280px-Andromeda_Galaxy_%28with_h-alpha%29.jpg";
public static final String SPRITE_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/a/a1/Glossy_3d_blue_blue2.png/"
+ "120px-Glossy_3d_blue_blue2.png";
private Image background;
private JLabel spriteLabel = new JLabel();
public SpriteOnBackground(Image bg, Image spriteImg) {
background = bg;
spriteLabel.setIcon(new ImageIcon(spriteImg));
spriteLabel.setSize(spriteLabel.getPreferredSize());
setLayout(null);
add(spriteLabel);
MyMouse myMouse = new MyMouse();
spriteLabel.addMouseListener(myMouse);
spriteLabel.addMouseMotionListener(myMouse);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet() || background == null) {
return super.getPreferredSize();
}
// make JPanel the size of the image
int w = background.getWidth(this);
int h = background.getHeight(this);
return new Dimension(w, h);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// draw background image
g.drawImage(background, 0, 0, this);
}
// mouse listener to drag the JLabel around the GUI
private class MyMouse extends MouseAdapter {
private Point p1;
private Point pSprite;
@Override
public void mousePressed(MouseEvent e) {
p1 = e.getLocationOnScreen();
pSprite = spriteLabel.getLocation();
}
@Override
public void mouseDragged(MouseEvent e) {
if (p1 != null) {
moveSprite(e);
}
}
private void moveSprite(MouseEvent e) {
Point p2 = e.getLocationOnScreen();
int x = pSprite.x + p2.x - p1.x;
int y = pSprite.y + p2.y - p1.y;
Point newP = new Point(x, y);
spriteLabel.setLocation(newP);
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
if (p1 != null) {
moveSprite(e);
}
p1 = null;
}
}
private static void createAndShowGui() {
SpriteOnBackground mainPanel = null;
try {
URL backgroundUrl = new URL(ANDROMEDA_IMAGE);
Image backGroundImg = ImageIO.read(backgroundUrl);
URL spriteUrl = new URL(SPRITE_IMAGE);
Image spriteImg = ImageIO.read(spriteUrl);
mainPanel = new SpriteOnBackground(backGroundImg, spriteImg);
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame("Sprite On Background");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
在早期版本的MySQL / MariaDB中,一种解决方案是使用相关子查询:
ROW_NUMBER()