MySQL data from multiple tables
MySQL data from multiple tables
mccarville
Posts: 4Questions: 0Answers: 0
I am very new to DataTables and I am quite frankly amazed at all of it's capabilitites.
I am trying to create a table that is getting it's data from MySql. Everything works great if I select from a single table in my database, but I really need to select multiple columns from mulitiple tables with WHERE clauses. In SQL I used:
[code]SELECT filename, week, dept, course, lp_dept.description, lp_weeks.year, lp_weeks.desc FROM lp_plans, lp_dept, lp_weeks WHERE username = '" .$_SESSION['MM_Username']. "' AND lp_plans.dept=lp_dept.id AND lp_plans.week=lp_weeks.id ORDER BY `timestamp` DESC"[/code]
For DataTables I am working from the Data Sources > Server Side Processing example, and was trying to do something like this:
[code]$aColumns = array( 'lp_weeks.year', 'lp_weeks.desc', 'lp_dept.description', 'lp_plans.course', 'lp_plans.filename');
$sIndexColumn = "lp_plans.ID";
$sTable = "lp_plans, lp_dept, lp_weeks";
$sWhere = "lp_plans.dept=lp_dept.ID AND lp_plans.week=lp_weeks.ID";[/code]
I honestly don't now if I am even on the right track for what I am trying to do. If anyone has some experience and can throw me a bone I would truly appreciate it!!
I am trying to create a table that is getting it's data from MySql. Everything works great if I select from a single table in my database, but I really need to select multiple columns from mulitiple tables with WHERE clauses. In SQL I used:
[code]SELECT filename, week, dept, course, lp_dept.description, lp_weeks.year, lp_weeks.desc FROM lp_plans, lp_dept, lp_weeks WHERE username = '" .$_SESSION['MM_Username']. "' AND lp_plans.dept=lp_dept.id AND lp_plans.week=lp_weeks.id ORDER BY `timestamp` DESC"[/code]
For DataTables I am working from the Data Sources > Server Side Processing example, and was trying to do something like this:
[code]$aColumns = array( 'lp_weeks.year', 'lp_weeks.desc', 'lp_dept.description', 'lp_plans.course', 'lp_plans.filename');
$sIndexColumn = "lp_plans.ID";
$sTable = "lp_plans, lp_dept, lp_weeks";
$sWhere = "lp_plans.dept=lp_dept.ID AND lp_plans.week=lp_weeks.ID";[/code]
I honestly don't now if I am even on the right track for what I am trying to do. If anyone has some experience and can throw me a bone I would truly appreciate it!!
This discussion has been closed.
Replies
Or you use explicit JOIN syntax, column aliases and adjust $aColumns. For JOIN syntax, see http://dev.mysql.com/doc/refman/5.1/de/join.html
Some code sniplets - of course not tested:
[code]
SELECT lp_plans.filename as filename, lp_dept.description as desctiption
FORM lp_plans p
LEFT JOIN lp_dept d ON p.dept = d.id
WHERE username = $_SESSION['MM_Username']
in php code:
$aColumns = array( 'filename', 'desctiption');
[/code]
STANDARD PHP MYSQL SELECT
[code]mysql_select_db($database_con1, $con1);
$query_Recordset1 = "SELECT filename, week, dept, course, lp_dept.description, lp_weeks.year, lp_weeks.desc FROM lp_plans, lp_dept, lp_weeks WHERE username = '" .$UN. "' AND lp_plans.dept=lp_dept.id AND lp_plans.week=lp_weeks.id ORDER BY `timestamp` DESC";
$Recordset1 = mysql_query($query_Recordset1, $con1) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);[/code]
CONVERT TO JSON
[code]{ "aaData": [
<?php if ($totalRows_Recordset1 > 0) { // Show if recordset not empty ?>
<?php
$firsttime = false;
do {
if ($firsttime) { echo ", "; } ?>
["<?php echo $row_Recordset1['year'];?>",
"<?php echo $row_Recordset1['desc']; ?>",
"<?php echo $row_Recordset1['description']; ?>",
"<?php echo $row_Recordset1['course']; ?>",
"Open"
]
<?php $firsttime = true;}
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<?php } // Show if recordset not empty ?>
]}[/code]
The if inside the do puts a comma in front of every row except the first one, thus removing the comma at the end of the last record.
[code]
oTable = $('#example').dataTable( {
[...]
"fnInitComplete": function() {
$("thead th.addSelects").each( function ( i ) {
this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
} )
}
[/code]