我有这个 MySQL 查询在 MySQL 环境中运行良好,但是在 PHP 中运行时不会因为它在每种情况下(记录)显示 REGION、PROVINCIA、COMUNA 的相同值,而不是属于每个独立记录的正确值,因为它们可能彼此不同。
我需要找到正确的 SQL 语句以允许我获取查询中涉及的每条记录 (3) 的实际 REGION、PROVINCIA、COMUNA 的方式进行查询。
表 pos1postul、pos2pad 和 pos3mad 上的记录通过字段 REGION_ID、PROVINCIA_ID、COMUNA_ID 引用表 dir_region、dir_provincia 和 dir_comuna。
任何帮助都感激不尽。
SELECT * FROM db3.pos1postul
INNER JOIN db3.dir_region AS region1 ON pos1postul.pos1_region=region1.REGION_ID
INNER JOIN db3.dir_provincia AS provincia1 ON pos1postul.pos1_prov=provincia1.PROVINCIA_ID
INNER JOIN db3.dir_comuna AS comuna1 ON pos1postul.pos1_comu=comuna1.COMUNA_ID, db3.pos2pad
INNER JOIN db3.dir_region AS region2 ON pos2pad.pos2_regionpad=region2.REGION_ID
INNER JOIN db3.dir_provincia AS provincia2 ON pos2pad.pos2_provpad=provincia2.PROVINCIA_ID
INNER JOIN db3.dir_comuna AS comuna2 ON pos2pad.pos2_comupad=comuna2.COMUNA_ID, db3.pos3mad
INNER JOIN db3.dir_region AS region3 ON pos3mad.pos3_regionmad=region3.REGION_ID
INNER JOIN db3.dir_provincia AS provincia3 ON pos3mad.pos3_provmad=provincia3.PROVINCIA_ID
INNER JOIN db3.dir_comuna AS comuna3 ON pos3mad.pos3_comumad=comuna3.COMUNA_ID
WHERE pos1_aluID='n'
AND pos2_padID=pos1_IDpostulpad
AND pos3_madID=pos1_IDpostulmad
PHP代码:
$idpostul_rsPostul = "-1";
if (isset($idpostul)) {
$idpostul_rsPostul = $idpostul;
}
mysql_select_db($database_conndb3, $conndb3);
$query_rsPostul = sprintf("SELECT * FROM db3.pos1postul INNER JOIN db3.dir_region AS region1 ON pos1postul.pos1_region=region1.REGION_ID INNER JOIN db3.dir_provincia AS provincia1 ON pos1postul.pos1_prov=provincia1.PROVINCIA_ID INNER JOIN db3.dir_comuna AS comuna1 ON pos1postul.pos1_comu=comuna1.COMUNA_ID, db3.pos2pad INNER JOIN db3.dir_region AS region2 ON pos2pad.pos2_regionpad=region2.REGION_ID INNER JOIN db3.dir_provincia AS provincia2 ON pos2pad.pos2_provpad=provincia2.PROVINCIA_ID INNER JOIN db3.dir_comuna AS comuna2 ON pos2pad.pos2_comupad=comuna2.COMUNA_ID, db3.pos3mad INNER JOIN db3.dir_region AS region3 ON pos3mad.pos3_regionmad=region3.REGION_ID INNER JOIN db3.dir_provincia AS provincia3 ON pos3mad.pos3_provmad=provincia3.PROVINCIA_ID INNER JOIN db3.dir_comuna AS comuna3 ON pos3mad.pos3_comumad=comuna3.COMUNA_ID WHERE pos1_aluID=%s AND pos2_padID=pos1_IDpostulpad AND pos3_madID=pos1_IDpostulmad", GetSQLValueString($idpostul_rsPostul, "int"));
$rsPostul = mysql_query($query_rsPostul, $conndb3) or die(mysql_error());
$row_rsPostul = mysql_fetch_assoc($rsPostul);