如何获得连接操作的结果?

时间:2013-01-18 08:18:41

标签: sql drupal join

每当我尝试在drupal的函数模块中执行此sql查询时,我无法获得结果,但是当我尝试在MySQL中执行此操作时,我可以查看结果。我的代码如下所示:

 function _get_subject_sub_category() {

  $options = array();
  $sql = "SELECT father.Subject_Code, child.Subject_Category 
FROM {subjects} as child 
INNER JOIN {subjects} as father ON (child.Parent_Category = father.Subject_Code
AND child.Level =2 )";

  $result = db_query($sql);
  foreach ($result as $row) {


     $options[$row->father.Subject_Code] = $row->child.Subject_Category;

  }
  return $options;
  }

我遇到的错误是在行$ options [$ row-> father.Subject_Code] = $ row-> child.Subject_Category;`中。

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

尝试更改此行:

$options[$row->father.Subject_Code] = $row->child.Subject_Category;

对此:

$options[$row->Subject_Code] = $row->Subject_Category;

表格的名称不在结果中。如果您需要避免混淆,可以在SQL查询中使用别名。

答案 1 :(得分:1)

我不知道drupal并且不使用php,但我会说:

删除 father.

中的$row->father.Subject_Code

child.

中的

$row->child.Subject_Category

因为父亲和孩子只是db别名。