欢迎各位兄弟 发布技术文章
这里的技术是共享的
function shipingzhongcustom_views_query_alter(&$view, &$query) { if ($view->name == 'allcontentview' && $view->current_display=='page_33') { $query->add_where(1, "node_data_field_is_baoming.field_is_baoming_value IS NULL or node_data_field_is_baoming.field_is_baoming_value <> '是' "); $query->add_where(1, "node_data_field_xueshenname.field_is_zx_shangmen_value = '是' "); $query->set_where_group('OR', 1); $query->add_where(2, "node_data_field_is_dingjing.field_is_dingjing_value IS NULL or node_data_field_is_dingjing.field_is_dingjing_value <> '是' "); $query->add_where(2, "node_data_field_xueshenname.field_is_zx_shangmen_value = '是' "); $query->set_where_group('OR', 2); } }
上门未报名的条件相当于
where (DATE_FORMAT(ADDTIME(node_data_field_shangmeng_time.field_shangmeng_time_value, SEC_TO_TIME(28800)), '%Y-%m-%d') <= '2017-02-20')
AND ((node_data_field_is_shangmen.field_is_shangmen_value = '是') AND (node.type in ('kefuchat')))
AND ((node_data_field_is_baoming.field_is_baoming_value IS NULL or node_data_field_is_baoming.field_is_baoming_value <> '是' ) OR (node_data_field_xueshenname.field_is_zx_shangmen_value = '是' ))
AND ((node_data_field_is_dingjing.field_is_dingjing_value IS NULL or node_data_field_is_dingjing.field_is_dingjing_value <> '是' ) OR (node_data_field_xueshenname.field_is_zx_shangmen_value = '是' ))
function shipingzhongcustom_views_query_alter(&$view, &$query)
{ if ($view->name == 'allcontentview' && $view->current_display=='page_33') { //$export = kprint_r($query->where, TRUE); // $vars - is a variable which you want to print. //print $export; //$query->add_where(1, "node_data_field_is_baoming.field_is_baoming_value IS NULL and node_data_field_is_dingjing.field_is_dingjing_value IS NULL "); $query->add_where(1, "node_data_field_is_baoming.field_is_baoming_value IS NULL or node_data_field_is_baoming.field_is_baoming_value <> '是' "); $query->add_where(1, "node_data_field_xueshenname.field_is_zx_shangmen_value = '是' "); $query->set_where_group('OR', 1); $query->add_where(2, "node_data_field_is_dingjing.field_is_dingjing_value IS NULL or node_data_field_is_dingjing.field_is_dingjing_value <> '是' "); $query->add_where(2, "node_data_field_xueshenname.field_is_zx_shangmen_value = '是' "); $query->set_where_group('OR', 2); //$export = kprint_r($query->where, TRUE); //print $export; } if ($view->name == 'allcontentview' && $view->current_display=='page_3') { //这里判断是否有值为 empty 如果有的话 就移除它 field_yixiangzhuanye_select_value 再增加一个 其值 为 is null 或 '' //这个empty 是哪里设置的呢 是 本文件约 1615行进行 form_alter 设置的 foreach($query->where[0]['args'] as $key=>$value){ if($value=='empty' && stristr($query->where[0]['clauses'][$key],'field_yixiangzhuanye_select_value')){ $export = kprint_r($query, TRUE); // $vars - is a variable which you want to print. print $export; $query->add_where(0, "node_data_field_yixiangzhuanye_select.field_yixiangzhuanye_select_value IS NULL or node_data_field_yixiangzhuanye_select.field_yixiangzhuanye_select_value='' "); unset($query->where[0]['args'][$key]); unset($query->where[0]['clauses'][$key]); }; // if($value=='empty' && stristr($query->where[0]['clauses'][$key],'field_yixiangzhuanye_select_value')){ // unset($query->where[0]['args'][$key]); // unset($query->where[0]['clauses'][$key]); //// //注意这里的字段啊 是 node_data_field_xueshenname_field_yixiangzhuanye_select_value 从 dsm($query)可以看出来这个对象的fields字段 // $query->add_where(1, "node_data_field_xueshenname_field_yixiangzhuanye_select_value.field_yixiangzhuanye_select_value IS NULL or node_data_field_xueshenname_field_yixiangzhuanye_select_value.field_yixiangzhuanye_select_value='' "); // //$query->add_where(1, "node_data_field_yixiangzhuanye_select_value_0.field_yixiangzhuanye_select_value IS NULL or node_data_field_yixiangzhuanye_select_value_0.field_yixiangzhuanye_select_value='' "); // $query->set_where_group('AND', 1); // //// $query->where[0]['clauses'][$key] = " (node_data_field_yixiangzhuanye_select_value_0.field_yixiangzhuanye_select_value IS NULL) or (node_data_field_yixiangzhuanye_select_value_0.field_yixiangzhuanye_select_value='') "; ////// $query->where[0]['clauses'][$key] = "node_data_field_yixiangzhuanye_select_value_0.field_yixiangzhuanye_select_value = '%s'"; //// unset($query->where[0]['args'][$key]); // // } } // $query->add_where(1, "node_data_field_yixiangzhuanye_select_value_0.field_yixiangzhuanye_select_value IS NULL or node_data_field_yixiangzhuanye_select_value_0.field_yixiangzhuanye_select_value='' "); // $query->set_where_group('AND', 1); // $export = kprint_r($query,TRUE); // $vars - is a variable which you want to print. // print $export; } }
第一种方法
Views Filtergroup
来自 https://www.drupal.org/project/views_filtergroup
第二种方法
add_where. For example see the sample code
function mymodule_views_query_alter(&$view, &$query) {
if ('yourviewname' == $view->name && 'page' == $view->current_display) {
$weight_parent = array(29,31);
$or = db_or()
->condition('taxonomy_term_data_taxonomy_term_hierarchy.weight', 'NULL', 'IS')
->condition('taxonomy_term_data_node.weight', $weight_parent, 'IN');
$query->add_where(1,$or);
}
}
This will create an OR in among two WHERE clauses. Now you can enable 'Show the SQL query in view settings(admin/structure/views) then you can check the updated query at the bottom of view settings page(admin/structure/views/view/your_view_name).
这将创建一个或两个WHERE子句中。现在,您可以启用“显示视图设置(管理/结构/人次)的SQL查询,那么你可以在视图设置页面(管理/结构/视图/视图/ your_view_name)的底部检查更新的查询。
"old query" AND ((taxonomy_term_data_taxonomy_term_hierarchy.weight IS 'NULL') OR (taxonomy_term_data_node.weight IN ('29', '31')))
Similarly you can use db_and to add AND condition. Hope this helps someone out there. Thanks!
同样,您可以使用db_and添加和条件。希望这有助于有人在那里。谢谢!
来自 http://www.zyxware.com/articles/3886/drupal-how-to-use-or-clause-in-hook-view-query-alter-in-drupal-7
# 为什么我自己写的sql 要用 '%Y-%m-%%d' (为什么要用两上百分号d)
select c_t_xiaonenzhibantime.nid,
count(field_xnzbt_start_time_value) as date_cou,
DATE_FORMAT(ADDTIME(field_xnzbt_start_time_value, SEC_TO_TIME(28800)), '%Y-%m-%%d') as date_one
from content_type_xiaonenzhibantime c_t_xiaonenzhibantime
inner join content_field_xnfx_person c_f_xnfx_person on c_f_xnfx_person.nid=c_t_xiaonenzhibantime.nid where
field_xnzbt_start_time_value>='2019-07-01'
and field_xnzbt_end_time_value<='2019-07-04 00:00:00'
group by DATE_FORMAT(ADDTIME(field_xnzbt_start_time_value, SEC_TO_TIME(28800)),
'%Y-%m-%%d') order by field_xnzbt_start_time_value asc
难道是因为 public_html/sites/all/modules/views/includes/handlers.inc 这个里面对mysqli的%d进行了处理?