0

编写自定义模块尝试查找评论。我有一个SQL语句如下:

    $client_comment = db_query("SELECT comment.subject AS comment_subject, 
comment.cid AS cid, 
comment.nid AS comment_nid, 
comment.created AS comment_created
FROM 
{comment} comment
INNER JOIN {node} node_comment ON comment.nid = node_comment.nid
INNER JOIN {field_data_field_associated_client} associated_client ON comment.cid = associated_client.entity_id
WHERE (( (associated_client.field_associated_client_nid = '$nid') 
OR (comment.nid = '$nid') ) 
AND (comment.status <> '0') 
AND (node_comment.status = '1') ) 
ORDER BY comment_created DESC
LIMIT 10 OFFSET 0");

理想情况下,这应该返回以下内容:

结果的屏幕截图 但是,我得到以下信息:

结果的屏幕截图 2

出于某种原因,它只返回“associated_client”结果而不是“comment.nid”结果。这是我想开始工作的完整声明。我知道这是一个 SMH 时刻,但任何帮助都会很棒。


根据请求,这是我用来循环返回数组的代码:

foreach($client_comment as $item) {
        print_r($item);
        print '<br /><br />';
    }

这是 2 个查询,它们分别将所有数据打印到屏幕上。我想将这两个查询合并为 1 个查询:

    $client_comment = db_query("SELECT comment.subject AS comment_subject, comment.cid AS cid, comment.nid AS comment_nid, comment.created AS comment_created
FROM 
{comment} comment
INNER JOIN {node} node_comment ON comment.nid = node_comment.nid
WHERE (( (comment.status <> '0') AND (node_comment.status = '1') AND (comment.nid = '$nid')  ))
ORDER BY comment_created DESC
LIMIT 10 OFFSET 0");
    // Get the comments associated TO the client from Caregiver node.
    $client_associated_comment = db_query("SELECT comment.subject AS comment_subject, comment.cid AS cid, comment.nid AS comment_nid, comment.created AS comment_created
FROM 
{comment} comment
INNER JOIN {node} node_comment ON comment.nid = node_comment.nid
INNER JOIN {field_data_field_associated_client} associated_client ON comment.cid = associated_client.entity_id
WHERE (( (comment.status <> '0') AND (node_comment.status = '1') AND (associated_client.field_associated_client_nid = '$nid')  ))
ORDER BY comment_created DESC
LIMIT 10 OFFSET 0");
    foreach($client_comment as $item) {
        $client_comment_array[] = $item;
    }
    foreach ($client_associated_comment as $item) {
        $client_comment_array[] = $item;
    }
    foreach($client_comment_array as $item) {
        print_r($item);
        print '<br /><br />';
    }
4

1 回答 1

0

好吧,我通过使用LEFT JOIN而不是解决了它INNER JOIN。结果查询:

$client_comment = db_query("SELECT comment.subject AS comment_subject, 
comment.cid AS cid, 
comment.nid AS comment_nid, 
comment.created AS comment_created
FROM 
{comment} comment
INNER JOIN {node} node_comment ON comment.nid = node_comment.nid
LEFT JOIN {field_data_field_associated_client} associated_client ON comment.cid = associated_client.entity_id
WHERE (( (associated_client.field_associated_client_nid = '$nid') 
OR (comment.nid = '$nid') ) 
AND (comment.status <> '0') 
AND (node_comment.status = '1') ) 
ORDER BY comment_created DESC
LIMIT 10 OFFSET 0");
    foreach($client_comment as $item) {
        print_r($item);
        print '<br /><br />';
    }
于 2013-04-03T19:10:52.173 回答