Datatables服务器端处理INNER JOIN或多个表
问题描述:
下面是我的代码,没有服务器端处理,但因为它会产生很多行并且会增长很多,所以我需要使用服务器端处理。Datatables服务器端处理INNER JOIN或多个表
数据库表(不包括所有字段,但这些是我想要的那些和领域INNER JOIN):
course_modules - id (pk), course(course.id), module(module.id), added(UNIXTIMESTAMP)
course - id (pk), category(course_categories.id), fullname(text)
course_categories- id (pk), name(text)
modules- id (pk), name(text)
我希望能够以显示UNIXTIMESTAMP的日期(“d /米/ Y');
将这项工作的INNER JOIN
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
INNER JOIN modules ON $sTable.module = modules.id
INNER JOIN course ON $sTable.course = course.id
INNER JOIN course_categories ON course.category = course_categories.id
$sWhere
$sOrder
$sLimit
";
原始的PHP版本,而服务器端的处理:
$mods = get_records_sql("SELECT * FROM {$CFG->prefix}course_modules");
foreach ($mods as $mod)
{
$thecourse = get_record('course','id',$mod->course);
$themodule = get_record('modules','id',$mod->module);
$thecat = get_record('course_categories','id',$thecourse->category);
echo '<tr>';
echo '<td>'.$thecat->name.'</td>';
echo '<td>'.$thecourse->fullname.'</td>';
echo '<td>'.$themodule->name.'</td>';
echo '<td>';
echo date('d/m/Y', $mod->added);
echo'</td>';
echo '</tr>';
}
我有服务器端处理沃金罚款与我datatables但不链接到其他表。此表只返回ID,我希望能够从上面演示的另一个表中获取值。
我需要使用innner join吗?如果是的话我怎么能与server_processing.php脚本合并: 我用过这个:http://datatables.net/release-datatables/examples/data_sources/server_side.html
或者我可以将我的上述方法合并到server_processing.php中。
任何指导将不胜感激。 在此先感谢。
答
问题的答案的相同目录logging.txt文件: 问题是有2 colu mn的名字相同,所以我在SQL查询中使用了alises。 也没有使用INNER JOIN。
列使用别名名和列名数组:
$aColumns = array('catname', 'fullname', 'modulename', 'added');
为4代表的SQL查询:
$sQuery = "
SELECT mdl_course.fullname, mdl_modules.name AS modulename, mdl_course_categories.name AS catname, mdl_course_modules.added
FROM mdl_course_modules, mdl_modules, mdl_course, mdl_course_categories
WHERE mdl_course_modules.module = mdl_modules.id AND mdl_course_modules.course = mdl_course.id AND mdl_course.category = mdl_course_categories.id
$sOrder
$sLimit
";
$rResult = mysql_query($sQuery, $gaSql['link']) or die(mysql_error());
答
是的,我认为你应该使用一个内连接(但我需要对表的更多细节写准确的查询),然后你在这里mustmodify的SQL
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query($sQuery, $gaSql['link']) or die(mysql_error());
编辑 - 记录查询做
$filename = __DIR__.DIRECTORY_SEPARATOR."logging.txt";
file_put_contents($filename, $sQuery, FILE_APPEND);
你应该在脚本
@:尼古拉Peluchetti:我已经修改与DB结构的问题。谢谢 – Codded 2012-03-26 11:07:09
@:Nicola Peluchetti:用我的INNER JOIN更新,这看起来对吗? – Codded 2012-03-27 11:21:20
@Codded是它应该工作,但在任何情况下,你应该记录查询(你可以使用firephp)并验证它在mysql中是正确的 – 2012-03-27 11:23:06