我想基于数组过滤数据,类似于" in"查询中的关键字:
SELECT count(*) as number_of_records,
count(CASE WHEN result = 'SUCCESS' THEN 1 END) as successful_builds_no,
min(CASE WHEN result = 'FAILURE' THEN 0 ELSE 1 END) as is_success,
min(duration) as best_duration,
max(build_date) as build_date
FROM mdl_selenium_results
WHERE build_date in ('2014-03-13', '2014-03-12')
GROUP BY build_date
如何使用Moodle DB api实现这一目标?
答案 0 :(得分:0)
如果要在Moodle中创建一个SQL查询,可以使用IN关键字,就像在示例中一样。
或者,为了使它更具交叉数据库兼容性,并且当列表中只有一个项目时使查询运行得更快,您可以写:
list($dsql, $dparam) = $DB->get_in_or_equal(array('2014-03-13', '2014-03-12'), SQL_PARAMS_NAMED);
$sql = "SELECT count(*) as number_of_records,
count(CASE WHEN result = 'SUCCESS' THEN 1 END) as successful_builds_no,
min(CASE WHEN result = 'FAILURE' THEN 0 ELSE 1 END) as is_success,
min(duration) as best_duration,
max(build_date) as build_date
FROM {selenium_results}
WHERE build_date $dsql
GROUP BY build_date";
$result = $DB->get_records_sql($sql, $dparams);
或者,如果您只想获取给定表中的所有匹配记录(然后担心在PHP中进行计算),您可以写:
$result = $DB->get_records_list('selenium_results', 'build_date', array('2014-03-13', '2014-03-12'));
(我没有运行上面的任何代码,所以对任何拼写错误道歉)