两个表中的SQL INSERT

时间:2015-05-17 17:42:15

标签: c# mysql sql sql-server insert

我有3张桌子:

Employee(Employee_ID, First_Name, Last_Name)
Project(Project_ID, Project_Name)     
Task(Employee_ID, Project_ID, Assigned_Project_Task)

我想将Employee_ID EmployeeTask插入Employee_ID=1 First_Name=ABC Last_Name=XYZ,当我将名字和姓氏分配给相应的ID时(例如Employee_ID)我首先给ABC名称和XYZ作为姓氏,1将放在Task表格的Project_ID字段中,ProjectTask表到Assigned_Project_Task的内容相同表格,最后我需要在Task表的Project_ID=10, Project_Name=SomeProject in Project 字段中为其命名。

  

EX。 Employee_ID = 1,First_Name = ABC,Last_Name =员工中的XYZ

first_name=ABC

如果我提供以下答案(在C#的Windows窗体中)last_name=XYZproject_name=SomeProjecttask=NewTask并命名为Task。 (我从windows窗体的txtbox中提供任务名称)

它会将Employee_ID=1, Project_ID=10, Assigned_Project_Name=NewTask 表放入以下内容:

private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        Socket socket = null;
        String ip;
        int port;
        String folder;

        ip = ipField.getText();
        port = (int) portField.getValue();
        folder = folderField.getText();

        try {
            File[] files = new File(folder).listFiles();
            System.out.println(files);
            socket = new Socket(InetAddress.getByName(ip), port);
            connectionStatus.setText("Connected");


        } catch (IOException ex) {
            Logger.getLogger(ConnectForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }       

如何插入?

1 个答案:

答案 0 :(得分:5)

您插入的语句应该如下所示

INSERT INTO Task (Employee_ID, Project_Id, Assigned_Project_Name)
SELECT e.Employee_Id , p.Project_Id, 'NewTask'
FROM Employees e INNER JOIN Projects p 
WHERE e.last_name='XYZ' AND e.first_name='ABC' 
AND p.Project_Name='SomeProject';

您需要了解如何在C#中构建此查询。