选择所有没有子项的行

时间:2011-02-16 17:43:51

标签: mysql

我真的在这一次失败了。

我有下表:

id    parentID     name
1     0            Frank
2     1            Sally
3     1            John
4     3            Beth

我想要一个只选择那些没有孩子的项目的语句,所以在前面的例子中:

Sally
Beth

结果会是什么。似乎无法在不创建递归函数的情况下找出执行此操作的查询,如果可以避免的话,我不想这样做。

4 个答案:

答案 0 :(得分:13)

select yt.name
    from YourTable yt
    where not exists (select null from YourTable where parentID = yt.id)

虽然效率较低(参见:Left outer join vs NOT EXISTS),但您也可以使用左连接执行此操作:

select yt1.name
    from YourTable yt1
        left join YourTable yt2
            on yt1.id = yt2.parentID
    where yt2.id is null

答案 1 :(得分:5)

select t.name
from that_table t
where t.id not in (select parentID from that_table);

答案 2 :(得分:1)

select person.ID, person.name
FROM table AS person
LEFT OUTER JOIN table AS child ON person.ID = child.parentID
WHERE child.parentID IS NULL

或效率较低

select person.ID, person.name, count(child.id) AS number_of_children
FROM table AS person
LEFT JOIN table AS child on person.ID = child.parentID
GROUP BY person.id
HAVING number_of_children = 0

答案 3 :(得分:0)

package com.mkyong.javafx.animatedball;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class BouncingBall extends Application{

    @Override
    public void start(Stage stage) {

        Pane canvas = new Pane();
        Scene scene = new Scene(canvas, 300, 300, Color.ALICEBLUE);
        Circle ball = new Circle(10, Color.CADETBLUE);
        ball.relocate(5, 5);

        canvas.getChildren().add(ball);

        stage.setTitle("Animated Ball");
        stage.setScene(scene);
        stage.show();

        Timeline timeline = new Timeline(new KeyFrame(Duration.millis(20),
                new EventHandler<ActionEvent>() {

            double dx = 7; //Step on x or velocity
            double dy = 3; //Step on y

            @Override
            public void handle(ActionEvent t) {
                //move the ball
                ball.setLayoutX(ball.getLayoutX() + dx);
                ball.setLayoutY(ball.getLayoutY() + dy);

                Bounds bounds = canvas.getBoundsInLocal();

                //If the ball reaches the left or right border make the step negative
                if(ball.getLayoutX() <= (bounds.getMinX() + ball.getRadius()) ||
                        ball.getLayoutX() >= (bounds.getMaxX() - ball.getRadius()) ){

                    dx = -dx;

                }

                //If the ball reaches the bottom or top border make the step negative
                if((ball.getLayoutY() >= (bounds.getMaxY() - ball.getRadius())) ||
                        (ball.getLayoutY() <= (bounds.getMinY() + ball.getRadius()))){

                    dy = -dy;

                }
            }
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();
    }

    public static void main(String[] args) {
        launch();
    }
}