4

我正在尝试使用以下代码从同一数据库查询中的两个数据库表中检索信息:

$query  =   db_select('webform_questionnaire_fieldset', 'wqf');
                        $query->join('webform_component', 'wc', 'wqf.cid = wc.cid');
                        $query->fields('wqf')
                        ->fields('wc')
                        ->condition('wqf.cid', $cid, '=')
                        ->condition('wqf.nid', $nid, '=')
                        ->condition('wqf.cid', 'wc.cid', '=')
                        ->execute()
                        ->fetchAll();

我得到的输出与数据库数据不相似,但看起来更像数据库元数据,如下所示:

SelectQuery::__set_state(array(
   'fields' => 
  array (
  ),
   'expressions' => 
  array (
  ),
   'tables' => 
  array (
    'wqf' => 
    array (
      'join type' => NULL,
      'table' => 'webform_questionnaire_fieldset',
      'alias' => 'wqf',
      'condition' => NULL,
      'arguments' => 
      array (
      ),
      'all_fields' => true,
    ),
    'wc' => 
    array (
      'join type' => 'INNER',
      'table' => 'webform_component',
      'alias' => 'wc',
      'condition' => NULL,
      'arguments' => 
      array (
      ),
      'all_fields' => true,
    ),
  ),
   'order' => 
  array (
  ), ......

谁能告诉我我做错了什么?

4

1 回答 1

5

添加condition到 join 语句。喜欢on table1.id = table2.id,然后添加$result = $query->execute()->fetchAll();

$query = db_select('webform_questionnaire_fieldset', 'wqf');
$query->join('webform_component', 'wc', 'wqf.cid = wc.cid');
$query->fields('wqf')
    ->fields('wc')
    ->condition('wqf.cid', $cid, '=')
    ->condition('wqf.nid', $nid, '=')
    ->condition('wqf.cid', 'wc.cid', '=');
$result = $query->execute()->fetchAll();

希望这会有所帮助……穆罕默德。

于 2012-07-18T14:13:24.077 回答