Ok so I have a table1 witch contains my main profile data.
Then I have table2 witch contains names(and type) of my custom user created fields (My admins can create custom fields for the main profile if they need)
And I have table3 that contains the values for the custom fields in table2. So table 3 refers to table2 for the field name and is linked to the table1 entry.
CREATE TABLE IF NOT EXISTS `table1` (
`id` int(11) NOT NULL auto_increment,
`numero_membre` varchar(255) NOT NULL default '',
`membre` tinyint(1) NOT NULL default '0',
(...ALOT OF FIELDS, I DONT FEEL THE NEED TO SHOW THEM ALL)
`ordering` int(10) NOT NULL default '0',
`dateModified` datetime NOT NULL default '0000-00-00 00:00:00',
`dateCreated` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `table2` (
`id` int(10) NOT NULL auto_increment,
`nom` varchar(255) NOT NULL default '',
`type` varchar(255) NOT NULL default '',
`published` tinyint(1) NOT NULL,
`ordering` int(10) NOT NULL,
`datemodified` datetime NOT NULL default '0000-00-00 00:00:00',
`datecreated` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
);
CREATE TABLE `table3` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`idchamps` VARCHAR(255) NOT NULL DEFAULT '',
`idprofil` VARCHAR(255) NOT NULL DEFAULT '',
`value` VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY( `id` )
);
So i want to SELECT all rows in table1 and i want it to join the name from table2 and make its value = from table3 that corresponds.
I was thinking LEFT JOIN. but that would seem to just add like all my table2 field names to the table1… This is complex… i hope you all understand.
If its not possible in 1 query… I think i have an alternative to get the data but this would make my life sooo much easier…
Objective: the User has an export data form. This form he can choose the fields from table1 and table2 that he wants for each entry (with checkboxes). then they can provide filters for ANY given field. such as id=1 or name LIKE %Somthing% to filters the results.
The checkbox values are saved in addfield[]
$this->getFields() returns ALL the fields so it can loop threw the filter post values.
HERE IS MY CODE THAT generates the SQL (part of a class):
function getExportObject() {
// --> FOR EXPORT
//Building Query
$postdata = JRequest::get( 'post' );
$exportFields = $postdata['addfield']; //Table 1
$exportCustomFields = $postdata['addcfield']; //table 2 name, table 3 value
$sqlSelect = '';
//Main profil Fields...
foreach($exportFields as $key => $field) {
if ($sqlSelect!='') $sqlSelect .= ', ';
$sqlSelect .= '`'.$field.'`';
}
//Add filters to the SELECT
//GET LIST OF ALL FIELDS....
$sqlFilter = '';
$loopFields = $this->getFields();
foreach($loopFields as $key => $field) {
if (JRequest::getVar('filter_'.$field)!='') {
if ($sqlFilter=='') { $sqlFilter .= " WHERE "; }
else { $sqlFilter .= " AND "; }
switch(JRequest::getVar('filter_'.$field)) {
case 'EQUAL' : $sqlFilter .= "`".$field."`='".JRequest::getVar('filter_val_'.$field)."'"; break;
case 'NOT_EQUAL' : $sqlFilter .= "`".$field."`!='".JRequest::getVar('filter_val_'.$field)."'"; break;
case 'CONTAINS' : $sqlFilter .= "`".$field."` LIKE '%".JRequest::getVar('filter_val_'.$field)."%'"; break;
case 'NOT_CONTAINS' : $sqlFilter .= "`".$field."` NOT LIKE '%".JRequest::getVar('filter_val_'.$field)."%'"; break;
default : die('Utilisation invalid. SVP. essayez de nouveau.'); break;
}
}
}
//PUTTING IT ALL TOGETTHER
//"SELECT t1.*, t2.nom, t3.value FROM table1 t1 LEFT JOIN table3 t3 INNER JOIN table2 t2 ON t3.fieldname = t2.nom ON t1.id = t3.idprofil"
$query = "SELECT ".$sqlSelect." FROM `table1`".$sqlFilter;
echo $query;
exit();
$this->_db->setQuery($query);
$exportList = $this->_db->loadObjectList();
return $exportList;
} //End of getExportObject()
So that function works for getting the values for the table1. i havnt set any of the code for the table2. but lets say table 2 varibles are held in the same way as table 1 but in the post varibale addcfield[]
Not 100% sure I understand the relationship between
table2andtable3, but I think you’re looking for something like this:EDIT: Modified query based on feedback in comments. Assuming the table2 id is added to table3.