数据库
首页 > 数据库> > mysql – Drupal 7 db_select嵌套选择查询

mysql – Drupal 7 db_select嵌套选择查询

作者:互联网

如何将嵌套的选择查询转换为Drual 7 db_select?以下是问题查询:

select 
    * 
from 
    tbl_word 
where 
    EngWord like '%apple%' or 
    WordID in (
        select 
            WordID 
        from 
            tbl_subword 
        where 
            EngWord like '%apple%'
    );

解决方法:

在语义上它非常简单:

$term = 'apple';

$sub_query = db_select('tbl_subword')
  ->fields('tbl_subword', array('WordID'))
  ->condition('EngWord', '%' . db_like($term) . '%'), 'LIKE');

$query = db_select('tbl_word')
  ->fields('tbl_word')
  ->condition(db_or()
    ->condition('EngWord', '%' . db_like($term) . '%', 'LIKE')
    ->condition('WordID', $sub_query, 'IN')
  );

$results = $query->execute()->fetchAll();

标签:mysql,sql,drupal,drupal-7,drupal-modules
来源: https://codeday.me/bug/20190620/1246471.html