如何使用db_select执行多列联接?有什么帮助吗?
以下是我正在寻找的示例。
SELECT * FROM digitaliq_flds_by_node a
JOIN digitaliq_fld_category b ON
a.fld_id = b.fld_id AND
a.fld_collection_id = b.fld_collection_id AND
a.fld_group_name = b.fld_group_name
如何使用db_select执行多列联接?有什么帮助吗?
以下是我正在寻找的示例。
SELECT * FROM digitaliq_flds_by_node a
JOIN digitaliq_fld_category b ON
a.fld_id = b.fld_id AND
a.fld_collection_id = b.fld_collection_id AND
a.fld_group_name = b.fld_group_name
Answers:
您可以使用以下代码。
$query = db_select('digitaliq_flds_by_node', 'a')->fields('a');
$alias = $query->join('digitaliq_fld_category', 'b', 'a.fld_id = %alias.fld_id AND a.fld_collection_id = %alias.fld_collection_id AND a.fld_group_name = %alias.fld_group_name');
$terms = $query->execute();
对SelectQuery :: join()的调用(类似的SelectQuery :: leftJoin(),SelectQuery :: innerJoin()和SelectQuery :: addJoin())不可链接,因为这些方法不会返回SelectQuery
对象,但是用于连接的实际别名。如果执行以下代码,则会出现错误。
$query = db_select('digitaliq_flds_by_node', 'a')
->fields('a')
->join('digitaliq_fld_category', 'b', 'a.fld_id = %alias.fld_id AND a.fld_collection_id = %alias.fld_collection_id AND a.fld_group_name = %alias.fld_group_name')
->execute();
PHP中的字符串不是对象,并且它们不定义execute()
方法。
为了验证代码是否正在执行正确的SQL查询,将其强制$query
转换为字符串并进行打印。您将获得将从您的代码执行的SQL查询。
$query = db_select('digitaliq_flds_by_node', 'a')->fields('a');
$alias = $query->join('digitaliq_fld_category', 'b', 'a.fld_id = %alias.fld_id AND a.fld_collection_id = %alias.fld_collection_id AND a.fld_group_name = %alias.fld_group_name');
print $query;
使用此代码,我得到以下输出。
SELECT a.*
FROM
{digitaliq_flds_by_node} a
INNER JOIN {digitaliq_fld_category} b ON a.fld_id = b.fld_id AND a.fld_collection_id = b.fld_collection_id AND a.fld_group_name = b.fld_group_name
尝试这个
$query = db_select('digitaliq_flds_by_node', 'a');
$query->join('digitaliq_fld_category', 'b', 'a.fld_id = b.fld_id');
$query->condition('a.fld_collection_id', 'b.fld_collection_id' '=');
$query->condition('a.fld_group_name', 'b.fld_group_name', '=');
$query->fields('a');
$query->execute();
:)
leftJoin()
不返回SelectQuery
对象。代码要求condition()
的不是对象。