Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下情况:
Table 1 group_id location Table 2 group_id empname
我需要以下输出:
group_id, location, empname 1 ABC NULL 1 ABC XYZ 1 ABC PQR
所以第一行是主行,其余行都是该主行的详细信息行。
我怎样才能得到这个输出?
TIA
博
SELECT t1.group_id,t1.location,NULL AS empname FROM Table1 t1 UNION SELECT t1.group_id,t1.location,t2.empname FROM Table1 t1 INNER JOIN Table2 t2 ON t1.group_id=t2.group_id ORDER BY 1,3
编辑: 只需将第一个 SQL 语句的第三列别名为empname.
empname