想要使用LIKE子句

时间:2016-03-13 09:29:09

标签: sql drupal drupal-7

我编写了一个查询,根据“投资组合”内容类型的“期限ID”获取节点标题。现在我想根据字符获取“节点标题”,这意味着我想使用“LIKE”以下查询的子句。如果用户输入“a”或“A”,则应该获取这些字符中的所有节点块。

<?php
$nodes = array();
$select = db_query("SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created
                    FROM {node} node
                    WHERE node.status = '1'
                         AND node.type = 'portfolio'
                         AND node.nid IN  (SELECT tn.nid AS nid
                                           FROM {taxonomy_index} tn
                                           WHERE tn.tid = '1')
                    ORDER BY node_created");
    foreach ($select as $nodes) {  
       print $nodes->node_title;
   }
?>

1 个答案:

答案 0 :(得分:0)

sql应该像

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created
    FROM {node} node
        WHERE node.status = '1'
            AND node.type = 'portfolio'
            AND node.nid IN  
            (
                SELECT tn.nid AS nid
                FROM {taxonomy_index} tn
                WHERE tn.tid = '1'
            )
            AND upper(node.title) like '%<whatever>%'
            ORDER BY node_created

您可以通过以下链接找到更多信息 https://drupal.stackexchange.com/questions/112530/how-to-use-like-condition-in-db-select-query
使用drupal提供的API来编写查询而不是像这种情况那样的原始sql

php mysqli prepared statement LIKE
如何使用LIKE准备好的语句(如果你不使用drupal提供的函数来完成工作)