需要我的表格为每个ID仅创建一行

时间:2019-03-27 19:04:33

标签: mysql sql

我需要连接两个表,

表1

public class CreateScheme extends Application {

    // Variablen
    ContextMenu contextMenu = new ContextMenu();
    MenuItem item1 = new MenuItem("Tooltip erstellen");
    private Model model;
    private Pane root;
    TableView<Model> tableview = new TableView<Model>();
    VBox vbox3 = new VBox();
    private Tooltip tooltip = new Tooltip();
    TableCell<Model, String> cell = new TableCell<>();


    @Override
    public void start(Stage secondStage) throws Exception {

        // Scene / root
        root = new Pane(); // Layout -> alle Elmente haben die Position 0/0
        Scene scene = new Scene(root, 800, 600);

        // Variablen erstellen
        VBox vbox = new VBox();
        VBox vbox2 = new VBox();
        HBox hbox = new HBox();
        HBox hbox2 = new HBox();
        HBox hbox3 = new HBox();
        Label label = new Label("Bewertungsschema erstellen: ");
        Button button = new Button("New");
        TextField textField1 = new TextField();
        CheckBox checkbox = new CheckBox();
        Button addButton = new Button("Erstellen");
        Button cancelButton = new Button("Abbrechen");
        Button saveButton = new Button("Speichern");
        Button addTextFieldButton = new Button("+");
        Button bold = new Button("F");
        Button redButton = new Button("Fehler markieren");


        ObservableList<Model> list = FXCollections.observableArrayList();

        model = new Model();
        model.addListener(e -> {
            while (e.next()) {
                if (e.wasAdded()) {
                    int ix = model.size() - 1; // fängt bei Überschrift Table 1 an.
                    for (Model m : list) {
                        if (m.size() <= ix) {
                            m.add("");
                        }
                    }
                    TableColumn<Model, String> column = new TableColumn<Model, String>("Table" + (ix + 1));

                    column.setCellFactory(TextFieldTableCell.<Model>forTableColumn());
                    column.setCellValueFactory(feature -> feature.getValue().get(ix));
                    column.setEditable(true);
                    tableview.getColumns().add(column);

                }
            }
        });

        cell.getStyleClass().add("cellmy");

        tableview.setItems(list);
        tableview.setEditable(true);

        textField1.setVisible(false);
        checkbox.setVisible(false);
        vbox2.setVisible(false);

        hbox.setSpacing(10.0);
        hbox.getChildren().addAll(button, textField1, checkbox);

        vbox.setLayoutX(10.0);
        vbox.setLayoutY(30.0);
        vbox.setSpacing(30.0);
        vbox.setPadding(new Insets(10.0, 20.0, 30.0, 10.0));
        vbox.getChildren().addAll(label, hbox, redButton, bold);

        vbox2.setLayoutX(300.0);
        vbox2.setLayoutY(30.0);
        vbox2.setSpacing(30.0);
        vbox2.setMaxHeight(200.0);
        vbox2.setPadding(new Insets(50.0, 30.0, 30.0, 30.0));
        vbox2.getChildren().add(tableview);

        hbox2.setLayoutX(10.0);
        hbox2.setLayoutY(500.0);
        hbox2.setSpacing(20.0);

        hbox3.setLayoutX(10.0);
        hbox3.setLayoutY(550.0);
        hbox3.setSpacing(20.0);
        hbox3.getChildren().addAll(addButton, cancelButton, saveButton, addTextFieldButton);

        contextMenu.getItems().addAll(item1);

        button.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent e) {
                textField1.setVisible(true);
                checkbox.setVisible(true);
            }
        });

        checkbox.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent e) {
                vbox2.setVisible(true);
                textField1.setEditable(false);
            }
        });

        addTextFieldButton.setOnAction(event -> {
            TextField textfield = new TextField();
            int ix = model.size();
            model.add("");
            textfield.textProperty().bindBidirectional(model.get(ix));
            hbox2.getChildren().add(textfield);
        });

        addButton.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent e) {

                // list.add(new Model());
                tableview.refresh();
            }
        });

        addButton.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                model.addTo(list);
                model.reset();
            }
        });

        saveButton.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                FileChooser fileChooser = new FileChooser();

                // Set extension filter
                FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
                fileChooser.getExtensionFilters().add(extFilter);

                // Show save file dialog
                File file = fileChooser.showSaveDialog(secondStage);

                if (file != null) {
                    saveTextToFile(root, file);
                }
            }
        });

        tableview.setTooltip(tooltip);
        item1.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                showInputTextDialog();


            }
        });

        tableview.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {

            @Override
            public void handle(ContextMenuEvent event) {
                contextMenu.show(tableview, event.getScreenX(), event.getScreenY());

            }
        });

        root.getChildren().addAll(vbox, vbox2, hbox2, hbox3);

        scene.getStylesheets().add("style.css");
        secondStage.setTitle("Bewertungsschema erstellen");
        secondStage.setScene(scene);
        secondStage.show();
    }

    private void showInputTextDialog() {

        TextInputDialog dialog = new TextInputDialog("Tran");

        dialog.setTitle("Bemerkung");
        dialog.setHeaderText("Meine Notiz:");
        dialog.setContentText("Bemerkung:");

        Optional<String> result = dialog.showAndWait();

        result.ifPresent(name -> {
            this.tooltip.setText(name);
        });

        if (result != null) {
            tableview.setStyle("-fx-background-color: red;");
        }
    }


    private void saveTextToFile(Pane pane2, File file) {
        try {
            PrintWriter writer;
            writer = new PrintWriter(file);
            writer.println(pane2);
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(CreateScheme.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Pane getPane2() {
        return this.root;
    }

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


public class Model {


    private final ObservableList<SimpleStringProperty> properties = FXCollections.observableArrayList();

    public void addListener(ListChangeListener<? super SimpleStringProperty> listener) {
        properties.addListener(listener);
    }

    public void add(String value) {
        properties.add(new SimpleStringProperty(value));
    }

    public int size() {
        return properties.size();
    }

    public Property<String> get(int ix) {
        return properties.get(ix);
    }

    public void addTo(List<Model> list) {
        Model copy = new Model();
        for (SimpleStringProperty prop : properties) {
            copy.add(prop.getValue());
        }
        list.add(copy);
    }

    public void reset() {
        for (SimpleStringProperty prop : properties) {
            prop.setValue("");
        }
    }
}

表2

t1_id | t1_text | t1_date

我想要得到什么:

t2_id | t2_text | t2_date | t1_id

表必须显示最新的唯一t1行及其链接的t2_text及其t2_date

这是我到目前为止所得到的:

t1_text | t1_date | t2_text | t2_date

它很近,但是没有雪茄。我仍然得到相同的t1行以多次显示

2 个答案:

答案 0 :(得分:0)

关于id字段的唯一性的说明:

select  t1.t1_id, t1.t1_date, t1.t1_text, t2.t2_id, t2.t2_date, t2.t2_text
from table_1 t1
inner join (SELECT  t1_id, max(t1_date) as date from table_1) t1m
   on t1.t1_id = t1m.t1_id and t1.t1_date = t1m.date
inner join table_2 t2
   on t1.t1_id = t2.t1_id
inner join (SELECT  t2_id, max(t2_date) as date from table_2) t2m
   on t2.t2_id = t2m.t2_id and t2.t2_date = t2m.date;

在此查询中,内联表选择具有最大日期的ID,然后将其与原始表结合以获取其余字段。

如果t1_id和t1_date的组合不是唯一的,仍然可能有重复的t1_id。但是在那种情况下,我不认为应该隐藏重复项,而应该解决它。


原始答案:

您需要使用内部联接,而不是左联接。

    SELECT `table_1`.`t1_text` AS 'Text', `table_1`.`t1_date` AS 't1_date', `table_2`.`t2_text` AS 't2_Text', `table_2`.`t2_date` AS 'Date'
    FROM `table_1`
    INNER JOIN `table_2`
    ON `table_1`.`t1_id`=`table_2`.`t1_id`
    ORDER BY `table_1`.`t1_date` DESC LIMIT 10

此外,您的某些引号字符不一致,不同的数据库可能需要使用不同的引号字符来分隔名称,但混合使用可能会给您带来麻烦。

答案 1 :(得分:0)

我不确定我是否清楚理解您想要的输出是什么。请尝试以下操作:

SELECT news.text AS 'News', news.date AS 'Publish date', 
comments.text AS 'Most Recent Comment', comments.date AS 'Comment Date' 
FROM news
JOIN (
    SELECT text, MAX(newsId) as newsId, date
    FROM comments 
    GROUP BY newsId
)comments ON news.newsId=comments.newsId
WHERE comments.text IS NOT NULL 
ORDER BY news.date DESC LIMIT 10