简体   繁体   English

如何在join语句中放置别名

[英]how to put alias inside join statement

hi I want to put an alias in my join statement. 您好,我想在我的join语句中放置一个别名。

my code is: 我的代码是:

SELECT distinct id_no, '' as picture, concat(lastname, IF(LENGTH(firstname) > 0 OR LENGTH(middlename) > 0, ', ', ''), concat(IF(p.degree = 5, 'Dr. ', IF(p.degree = 6, 'Dr. ', IF(p.degree = 28, 'Dr. ', '')))), firstname, IF(LENGTH(middlename) > 0, CONCAT(' ', middlename), '')) as name, tp.profession, '' as cv, e.entry_date, e.termination_date 
FROM (bims_people as p)

LEFT JOIN bims_people_education as bpe ON bpe.people_id = p.id_no 

LEFT JOIN tcms_profession as tp ON tp.profession_id = bpe.profession 

LEFT JOIN (select * from bims_people_employment_contract_data order by termination_date DESC) e ON e.people_id = p.id_no 

WHERE p.marked = 0 AND p.id_no > 1 GROUP BY p.id_no ORDER BY lastname ASC LIMIT 100, 50 ;

and I want it this way: 我想要这样:

SELECT distinct id_no, '' as picture, concat(lastname, IF(LENGTH(firstname) > 0 OR LENGTH(middlename) > 0, ', ', ''), concat(IF(p.degree = 5, 'Dr. ', IF(p.degree = 6, 'Dr. ', IF(p.degree = 28, 'Dr. ', '')))), firstname, IF(LENGTH(middlename) > 0, CONCAT(' ', middlename), '')) as name, tp.profession, '' as cv, e.entry_date, e.termination_date 
FROM (bims_people as p)

LEFT JOIN bims_people_education as bpe ON bpe.people_id = p.id_no 

LEFT JOIN tcms_profession as tp ON tp.profession_id = bpe.profession 

LEFT JOIN (select * from bims_people_employment_contract_data **where people_id = p.id_no** order by termination_date DESC limit 1) e ON e.people_id = p.id_no 

WHERE p.marked = 0 AND p.id_no > 1 GROUP BY p.id_no ORDER BY lastname ASC LIMIT 100, 50 ;

i keeps telling me that p.id_no is unknown; 我一直告诉我p.id_no是未知的;

 LEFT JOIN (select * from bims_people_employment_contract_data where people_id = p.id_no order by termination_date DESC limit 1) e ON e.people_id = p.id_no 

You cannot use a table's alias (correlation name) to define another table to JOIN to it. 您不能使用表的别名(关联名)来定义另一个表以将其联接。 After you have given two tables & aliases to JOIN, ON or WHERE can use the alias in conditions. 为JOIN提供两个表和别名后,ON或WHERE可以在条件中使用别名。

From MySQL 5.7 Reference Manual 14.2.10.8 Subqueries in the FROM Clause : 来自MySQL 5.7参考手册14.2.10.8 FROM子句中的子查询

Subqueries in the FROM clause cannot be correlated subqueries, unless used within the ON clause of a JOIN operation. 除非在JOIN操作的ON子句中使用,否则FROM子句中的子查询不能是相关子查询。

(What do you expect the quoted OUTER JOIN to do? Since p.id_no doesn't appear in an ON or WHERE, there's no particular row of a table p that it could be the id_no value of.) (您希望带引号的OUTER JOIN做什么?由于p.id_no不会出现在ON或WHERE中,因此表p中没有特定行可能是id_no值。)

Maybe you want: 也许你想要:

LEFT JOIN (SELECT * FROM bims_people_employment_contract_data
    WHERE (people_id, termination_date) IN
        (SELECT people_id, MAX(termination_date) AS termination_date
        FROM bims_people_employment_contract_data
        GROUP BY people_id)
    ) e
ON e.people_id = p.id_no

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM