Drupal 7 WHERE使用OR和AND

时间:2013-04-03 18:28:23

标签: php mysql drupal-7

编写自定义模块以查找注释。我有一个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");

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

Screen Shot of results 但是,我得到以下内容:

Screen shot of results 2

由于某种原因,它只返回“associated_client”结果而不是“comment.nid”结果。以下是我想要完成的完整陈述。我知道这是一个SMH时刻,但任何帮助都会很棒。


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

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

以下是2个查询,分别将所有数据打印到屏幕上。我想将这两个查询合并到一个查询中:

    $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 />';
    }

1 个答案:

答案 0 :(得分: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 />';
    }