字段名称相同时的SQL连接

时间:2010-11-25 20:43:46

标签: sql mysql drupal join

我在节点和url_alias中都有字段“language”。 当我进行转储时,第二个“语言”变量会覆盖第一个。 我如何识别这两个变量?

$string =   "SELECT * FROM {node} as node " .
            " LEFT JOIN {url_alias} as url " .
            " ON url.src = CONCAT('node/', node.nid) " .                
            " ORDER BY node.type , node.nid " ;

$result= db_query($string);
while ($row = db_fetch_object($result)){
echo $row->language;
var_dump($row);

1 个答案:

答案 0 :(得分:6)

SELECT子句中明确指定列名,而不是使用*,并为任何重复的列名提供别名。如,

SELECT node.nid,
    node.language as NodeLanguage,
    url.language as UrlLanguage
FROM {node} as node 
LEFT JOIN {url_alias} as url  
ON url.src = CONCAT('node/', node.nid)                 
ORDER BY node.type , node.nid