我正在尝试为Reimbursersement系统编写一个查询,该系统根据ID检索单个请求。该查询连接用户,请求类型和请求状态的其他三个表,在请求表中连接其外键,但我尝试连接两次用户表以提取两个不同的名称。
我的查询发布在下面:
SELECT ers_reimbursements.*,
ers_users.u_firstname AS author_fname, ers_users.u_lastname AS author_lname,
ers_users.u_firstname AS resolver_fname, ers_users.u_lastname AS resolver_lname,
ers_reimbursement_type.rt_type, ers_reimbursement_status.rs_status
FROM ers_reimbursements
INNER JOIN ers_users ON ers_users.u_id = ers_reimbursements.u_id_author
INNER JOIN ers_users ON ers_users.u_id = ers_reimbursements.u_id_resolver
INNER JOIN ers_reimbursement_type ON ers_reimbursement_type.rt_id = ers_reimbursements.rt_type
INNER JOIN ers_reimbursement_status ON ers_reimbursement_status.rs_id = ers_reimbursements.rt_status
WHERE ers_reimbursements.r_id = 2;
结果视图是:
R_ID | R_Amount | R_Description| R_Submitted | R_Resolved | U_ID_Author | U_ID_Resolver | RT_Type | RT_Status | Author_FName | Author_LName | Resolver_FName | Resolver_LName | RT_Type_1 | RS_Status
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 | 100 | AVS | 31-DEC-17 | 1-JAN-18 | 3 | 2 | 1 | 2 | James | Mitchell | James | Mitchell | Travel | Approved
在视图中,作者的ID为3,解析器ID为2. 3属于James Mitchell,2属于Alan Martin,但作者和解析器名称列均显示"詹姆斯米切尔,"何时它应该将解析器显示为" Alan Martin。"
为什么使用James'名字两次?我怎样才能得到James'和艾伦的名字出现在结果中?
谢谢。
答案 0 :(得分:0)
如果您多次加入ers_users
表,则必须使用别名来引用ers_users
表。
在下面的示例中,SELECT ers_reimbursements.*,
A.u_firstname AS author_fname, A.u_lastname AS author_lname,
R.u_firstname AS resolver_fname, R.u_lastname AS resolver_lname,
ers_reimbursement_type.rt_type, ers_reimbursement_status.rs_status
FROM ers_reimbursements
INNER JOIN ers_users A ON A.u_id = ers_reimbursements.u_id_author
INNER JOIN ers_users R ON R.u_id = ers_reimbursements.u_id_resolver
INNER JOIN ers_reimbursement_type ON ers_reimbursement_type.rt_id = ers_reimbursements.rt_type
INNER JOIN ers_reimbursement_status ON ers_reimbursement_status.rs_id = ers_reimbursements.rt_status
WHERE ers_reimbursements.r_id = 2;
被别名为' A'首先得到作者的名字,然后作为' R'引用它以检索解析器名称。
getMeals (page:number): Observable<any> {
let httpParams = new HttpParams().set('where', '%')
.set('orderBy', 'meal_name')
.set('page', page.toString())
.set('items', '10');
//this.messageService.add('Debug: ' + httpParams);
return this.http.get(this.mealUrl, { params: httpParams, observe: 'response'} )
.pipe(
//tap(meals => { console.log(meals.headers) }),
catchError(this.handleError<any>('getMeals'))
);
};
答案 1 :(得分:0)
您需要对连接进行别名以区分它们并使用字段引用中的别名:
id