sql中的三表联接

时间:2019-02-11 13:27:59

标签: sql sql-server

我正试图联接三个表。

表格:

  • HumanResources.Employees:雇员ID(主键),名字,职称-也称为Employee_Title,
  • ProjectDetails.TimeCards:员工ID(外键),项目ID(外键)
  • ProjectDetails.Projects:Project_Name,Project_ID(主键)

我尝试使用临时表加入他们。

select b.First_Name, b.Title, c.Project_ID from HumanResources.Employees b -- select statement
inner join ProjectDetails.TimeCards c on b.Employee_ID = c.Employee_ID -- first join seems to be the only one working.
inner join (select d.Project_Name as Project_Name, Project_ID from ProjectDetails.Projects d) as d on d.Project_ID=c.Project_ID -- second join doesn't seem to work.

3 个答案:

答案 0 :(得分:1)

子查询的使用充其量是多余的,而且“ d”的通用别名也可能是错误的根源。

只需:

public class WorkGridAdapter extends BaseAdapter {
    private ArrayList<AshaTransactions> list = new ArrayList<>();
    private Context context;
    Queries queries;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public WorkGridAdapter(ArrayList<AshaTransactions> list, Context context) {
        this.list=list;
        this.context=context;
        queries=new Queries(context);
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View grid;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            grid = new View(context);
            grid = inflater.inflate(R.layout.work_report_grid_cell, null);
            TextView gridcell,gridcell1,gridcell2,gridcell3,gridcell4,gridcell5,gridcell6;
            gridcell = grid.findViewById(R.id.gridcell);
            gridcell1 = grid.findViewById(R.id.gridcell1);
            gridcell2 = grid.findViewById(R.id.gridcell2);
            gridcell3 = grid.findViewById(R.id.gridcell3);
            gridcell4 = grid.findViewById(R.id.gridcell4);
            gridcell5 = grid.findViewById(R.id.gridcell5);
            gridcell6 = grid.findViewById(R.id.gridcell6);

            if(list.size()!=0){
                ArrayList<Individual> individualObjList = queries.getIndividualByIdIndividual(Long.parseLong(String.valueOf(list.get(position).getIdIndividual())));
                Individual individualObj = individualObjList.get(0);
                gridcell.setText(String.valueOf(position+1));
                gridcell1.setText(individualObj.getName().toString());
                gridcell2.setText(individualObj.getFatherName());
                if(individualObj.getIdGender()==1){
                    gridcell3.setText("M");
                }else{
                    gridcell3.setText("F");
                }
                gridcell4.setText(sdf.format(list.get(position).getDateOfTransaction()));
                gridcell5.setText(String.valueOf(list.get(position).getIncentiveForActivity()));
                gridcell6.setText(String.valueOf(list.get(position).getIncentiveForActivity()));
            }
        } else {
            grid = (View) convertView;
        }

        return grid;
    }

}

答案 1 :(得分:1)

在内部查询Project_ID时,使用列别名,然后加入别名列名。还要为子查询尝试使用其他别名。

答案 2 :(得分:1)

我看不到在查询中使用子查询的好处。您可以尝试以下查询吗?

确保您的表中的Projects具有匹配的Project_ID,否则当然不会出现任何问题。

SELECT b.First_Name
    ,b.Title
    ,c.Project_ID
FROM HumanResources.Employees b
INNER JOIN ProjectDetails.TimeCards c ON b.Employee_ID = c.Employee_ID
INNER JOIN ProjectDetails.Projects d ON d.Project_ID = c.Project_ID