我有一个从3个表a,b,c组合而成的查询,这里的问题是表c存在于2个数据库D1和D2中,因此我必须从两个数据库中获取表c的记录。表a和b仅来自数据库D1,

查询是:

$strQuery="SELECT a.id, b.lastname, school " ."FROM D1.a,D1.b" .
    " WHERE a.idint = (select c_idint from D1.c where Cid IN(10)
UNION select c_idint from D2.c where Cid IN(10) order by c_idint)
 AND a.idint = b.idb";


问题是字段“学校”来自表c,如果我避免查询中的“学校”字段工作正常,但我也必须获取字段“学校”,如何修改查询。谢谢:)

最佳答案

尝试这个:

$strQuery="SELECT a.id, b.lastname, c.school as dSchool, '' as ddSchool " ."FROM D1.a,D1.b, D1.c" .
    " WHERE a.idint = c.idint AND c.Cid IN (10)
 AND a.idint = b.idb

UNION

SELECT a.id, b.lastname, '' as dSchool, c.school as ddSchool " ."FROM D1.a,D1.b, D2.c" .
    " WHERE a.idint = c.idint AND c.Cid IN (10)
 AND a.idint = b.idb
";

08-17 16:57