Displaying results from multiple tables SQL/PHP - php

I currently have a database with 12 tables. I am doing a php query to pull the information from the database but I am not getting anything displayed. The query alone bridges all the tables starting with table schedule that have a foreign key related. Should I need to start the query from the table class and bridge with other tables?
TABLE DESIGN- PICTURE
If you like to duplicate my design- QUERY
$query = ("SELECT class_name, class_caption, class_credit_hours, class_description
FROM schedule
INNER JOIN section
ON class.id = section.class_id
INNER JOIN faculty
ON faculty.id = section.faculty_id
INNER JOIN faculty
ON faculty.id = office_hours.faculty_id
INNER JOIN faculty_titles
ON faculty_titles.faculty_id = faculty.id
INNER JOIN faculty_education
ON faculty_education.faculty_id = faculty.id
INNER JOIN section
ON section.faculty_id = faculty.id
INNER JOIN class
ON class.id = section.class_id
INNER JOIN major_class_br
ON major_class_br.class_id = class.id
INNER JOIN major_minor
ON major_class_br.major_minor_id = major_minor.id
");
//execute query
$result = mysql_query($query);
if ($result){
$totalhours = 0;
while ($row = mysql_fetch_assoc( $result ))
{
print "<b>" . $row['class_name'] . "</b><br>";
print $row['class_caption'] . "<br>";
print $row['class_description'] . "<br>";
print $row ['class_credit_hours'] . "hrs. <br>";
print "------------------------------<br />";
$totalhours += $row['class_credit_hours'];
}
}
SQL fiddle query

SELECT class_name, class_caption, class_credit_hours, class_description
FROM schedule
INNER JOIN section
ON class.id = section.class_id
Right here there's a problem: you are doing a INNER JOIN using the field 'class.id' but the table 'class' isn't in either side of the JOIN. So it won't work.
The query should start like this:
SELECT class_name, class_caption, class_credit_hours, class_description
FROM class
INNER JOIN section
ON class.id = section.class_id
And then do the JOIN with the table 'schedule' with the table it shares a common index (I guess it would be class).
The complete query should be something like this:
SELECT class.class_name, class.class_caption, class.class_credit_hours, class.class_description
FROM class
INNER JOIN section
ON class.id = section.class_id
INNER JOIN faculty
ON faculty.id = section.faculty_id OR faculty.id = office_hours.faculty_id
INNER JOIN faculty_titles
ON faculty_titles.faculty_id = faculty.id
INNER JOIN faculty_education
ON faculty_education.faculty_id = faculty.id
INNER JOIN major_class_br
ON major_class_br.class_id = class.id
INNER JOIN major_minor
ON major_class_br.major_minor_id = major_minor.id
INNER JOIN sched_sect_br
ON sched_sect_br.section_id = section.id
INNER JOIN schedule
ON schedule.id = sched_sect_br.schedule_id
INNER JOIN semester
ON semester.id = schedule.semester_id
INNER JOIN office_hours
ON schedule.id = office_hours.schedule_id AND faculty.id = office_hours.faculty_id
This query gets info from all the tables you have in your graph, aside from the event table, that isn't related to any other. But still this query should have more fields on the SELECT (you are only selection fields from the class table, I understand you'd want data from the other 10 tables as well), to do this simple add 'tablename.field' to the list of fields from the SELECT.

Related

INNER JOIN with multiple tabels

I Have PHP Script That SELECT FROM 7 Tabels and client_id is the Common Column
And Return Single Tebel With all the clients by as there client_id row after row, now The problem is the if there is in one Tabel tow rows with 2 duplicate id That's print duplicate rows like 100 times
maybe i don't do INNER JOIN right or something if someone has an idea how to prevent this
$stmt = $pdo->query("SELECT * FROM client_form cf
INNER JOIN client_form_2 cf2 ON cf.client_id = cf2.client_id
INNER JOIN client_form_3 cf3 ON cf.client_id = cf3.client_id
INNER JOIN client_form_4 cf4 ON cf.client_id = cf4.client_id
INNER JOIN client_form_5 cf5 ON cf.client_id = cf5.client_id
INNER JOIN client_form_6 cf6 ON cf.client_id = cf6.client_id
INNER JOIN client_form_7 cf7 ON cf.client_id = cf7.client_id
");

how to update multiple (5) tables using left join?

I always use left join in drop select and I dont know yet how to use it on update.
I've search a lot but I see only two tables. Im confuse when applying it in three or more table update.
Please check my query:
public function updateUser($edit_id,$username)
{
$stmt=$this->conn->prepare("UPDATE tbl_login LEFT JOIN activity_logs ON tbl_login.username = activity_logs.activity_logs,
LEFT JOIN tbl_files ON tbl_login.username = tbl_files.file_uploader,
LEFT JOIN tbl_manfiles ON tbl_login.username = tbl_manfiles.file_uploader,
LEFT JOIN tbl_section ON tbl_login.username = tbl_section.creator,
LEFT JOIN tbl_adfiles ON tbl_login.username = tbl_adfiles.adfile_uploader
SET tbl_login.username=:username
WHERE id=:id");
$stmt->execute(array(":id"=>$edit_id, ":username"=>$username));
return $stmt;
}
$query = "UPDATE profiledata t1 JOIN profileprivacy t2 ON (t1.uid = t2.uid)
SET t1.aboutyou = '$aboutyou', t1.quotes = '$quotes',
t2.aboutyouPrivacy = '$aboutyouPrivacy', t2.quotesPrivacy = '$quotesPrivacy'
WHERE t1.uid = '$sess_uid'";
update two tables at once

PHP, Mysql sub query with where clause

I have a query that display all my CLASS details, now I want a sub query that will count all enrolled student in that class.
I tried using two separate query and trying to merge them but I'm not successful doing so.
This is my query so far:
$str = "SELECT
class.id,
class.code AS classcode,
section.name AS sectionname,
subject.code,
class.units,
sched.name AS schedule,
class.slots,
class.dissolved,
(SELECT
Count(enrolldet.enrollno)
FROM
enrolldet
Inner Join enroll ON enrolldet.enrollno = enroll.enrollno
Inner Join class ON enrolldet.class = class.id
WHERE
enroll.validated = '1' AND
class.id = class.id) AS enrolled
FROM
class
Left Join sched ON class.sched = sched.id
Left Join section ON class.section = section.id
Left Join subject ON class.subject = subject.id
";
You don't need all those joins in the subquery. This should suffice:
(SELECT Count(*)
FROM enrolldet ed JOIN
enroll e
ON ed.enrollno = e.enrollno
WHERE e.validated = 1 AND
ed.class = c.id
) AS enrolled
That is, you don't need class in the subquery. And, I'm guessing that validated is a number so I removed the single quotes around "1".
You can't have the class table of both the main query and the subquery named class, because the subquery has access to both tables and this results in the SQL engine being confused due to the ambiguity.
To fix your query, give an alias, like cls, the inner one and change the AND clause to:
AND `class`.`id` = `cls`.`id`
Also, note that your subquery can be simplified by removing the inner class table altogether, since the subquery can access the class table of the main query, as already mentioned.
Code:
SELECT
`class`.`id`,
`class`.`code` AS `classcode`,
`section`.`name` AS `sectionname`,
`subject`.`code`,
`class`.`units`,
`sched`.`name` AS `schedule`,
`class`.`slots`,
`class`.`dissolved`,
(
SELECT COUNT(`enrollno`)
FROM `enrolldet`
INNER JOIN `enroll`
ON `enrolldet`.`enrollno` = `enroll`.`enrollno`
WHERE `enroll`.`validated` = '1'
AND `enrolldet`.`class` = `class`.`id`
) AS `enrolled`
FROM `class`
LEFT JOIN `sched` ON `class`.`sched` = `sched`.`id`
LEFT JOIN `section` ON `class`.`section` = `section`.`id`
LEFT JOIN `subject` ON `class`.`subject` = `subject`.`id`
$str = "SELECT
c.id,
c.code AS classcode,
section.name AS sectionname,
subject.code,
c.units,
sched.name AS schedule,
c.slots,
c.dissolved,
(SELECT
Count(e.enrollno)
FROM
enrolldet AS e
Inner Join enroll ON e.enrollno = enroll.enrollno
Inner Join class ON e.class = class.id
WHERE
enroll.validated = '1' AND
class.id = c.id) as enrolled
FROM
class AS c
Left Join sched ON c.sched = sched.id
Left Join section ON c.section = section.id
Left Join subject ON c.subject = subject.id";
i got the correct answer using this. for future reference. i just added aliases for each table

MYSQL Inner Join speed issue

I have been having mega issues with this query.
It's a file that's run by cron every 24 hours and runs conditional checks against all members in the database.
Apparently it crashes the MYSQL server (takes 2 hours to execute?).
Every table has primary keys set on rows foo_id & foo_uid so the joins are indexed under PRIMARY and as such should be good for speed.
please help, this is killing me.
$members = new WA_MySQLi_RS("members", $alpha, 1);
$members->setQuery("SELECT
registration.*,
child_base_survey.*,
child_base_scas.*,
child_base_smqf.*,
parent_base_survey.*,
parent_base_ippa.*,
parent_base_eac.*,
parent_base_scas.*,
parent_base_smqf.*,
parent_base_eval.*,
user_access_level.*,
parent_one_month_survey.*,
parent_one_month_ippa.*,
parent_one_month_eac.*,
parent_one_month_eval.*,
child_three_month_survey.*,
child_three_month_scas.*,
child_three_month_smqf.*,
parent_three_month_survey.*,
parent_three_month_scas.*,
parent_three_month_smqf.*,
parent_three_month_eval.*,
cron.*
FROM registration
INNER JOIN child_base_survey ON registration.rego_parent_uid = child_base_survey.child_base_survey_uid
INNER JOIN child_base_scas ON child_base_survey.child_base_survey_uid = child_base_scas.child_base_scas_uid
INNER JOIN child_base_smqf ON child_base_scas.child_base_scas_uid = child_base_smqf.child_base_smqf_uid
INNER JOIN parent_base_survey ON child_base_smqf.child_base_smqf_uid = parent_base_survey.parent_base_survey_uid
INNER JOIN parent_base_ippa ON parent_base_survey.parent_base_survey_uid = parent_base_ippa.parent_base_ippa_uid
INNER JOIN parent_base_eac ON parent_base_ippa.parent_base_ippa_uid = parent_base_eac.parent_base_eac_uid
INNER JOIN parent_base_scas ON parent_base_eac.parent_base_eac_uid = parent_base_scas.parent_base_scas_uid
INNER JOIN parent_base_smqf ON parent_base_scas.parent_base_scas_uid = parent_base_smqf.parent_base_smqf_uid
INNER JOIN parent_base_eval ON parent_base_smqf.parent_base_smqf_uid = parent_base_eval.parent_base_eval_uid
INNER JOIN user_access_level ON parent_base_eval.parent_base_eval_uid = user_access_level.user_access_level_uid
INNER JOIN parent_one_month_survey ON user_access_level.user_access_level_uid = parent_one_month_survey.parent_one_month_survey_uid
INNER JOIN parent_one_month_ippa ON parent_one_month_survey.parent_one_month_survey_uid = parent_one_month_ippa.parent_one_month_ippa_uid
INNER JOIN parent_one_month_eac ON parent_one_month_ippa.parent_one_month_ippa_uid = parent_one_month_eac.parent_one_month_eac_uid
INNER JOIN parent_one_month_eval ON parent_one_month_eac.parent_one_month_eac_uid = parent_one_month_eval.parent_one_month_eval_uid
INNER JOIN child_three_month_survey ON parent_one_month_eval.parent_one_month_eval_uid = child_three_month_survey.child_three_month_survey_uid
INNER JOIN child_three_month_scas ON child_three_month_survey.child_three_month_survey_uid = child_three_month_scas.child_three_month_scas_uid
INNER JOIN child_three_month_smqf ON child_three_month_scas.child_three_month_scas_uid = child_three_month_smqf.child_three_month_smqf_uid
INNER JOIN parent_three_month_survey ON child_three_month_smqf.child_three_month_smqf_uid = parent_three_month_survey.parent_three_month_survey_uid
INNER JOIN parent_three_month_scas ON parent_three_month_survey.parent_three_month_survey_uid = parent_three_month_scas.parent_three_month_scas_uid
INNER JOIN parent_three_month_smqf ON parent_three_month_scas.parent_three_month_scas_uid = parent_three_month_smqf.parent_three_month_smqf_uid
INNER JOIN parent_three_month_eval ON parent_three_month_smqf.parent_three_month_smqf_uid = parent_three_month_eval.parent_three_month_eval_uid
INNER JOIN cron ON parent_three_month_eval.parent_three_month_eval_uid = cron.cron_uid WHERE registration.rego_parent_uid = ?");
$members->bindParam("s", "" . ((isset($_SESSION["rego_parent_uid"])) ? $_SESSION["rego_parent_uid"] : "") . "", "-1"); //WAQB_Param1
$members->execute();
?>
Run them as separate queries, from a cursory glance, it looks like you are just joining every possible table with data associated with a reference to an id value. Your results are going to be the cross product of every matching row in each table. For N tables, each row in table X will be repeated r0*r1*r2*....rX-1*rX+1*...rN. With the 23 tables there, if each only had 2 rows, you'd have nearly 8.4 million rows in your results.

insert php code in sql query

function search_num_rows($param){
$company_name=$param['company_name'];
$loan_no=$param['loan_no'];
$q = $this->db->query("select Count(0) as num_rows
from contact_new
inner join companies c on contact_new.company_id = c.id
inner join history on contact_new.id = history.receiver_email
inner join escalation_level on contact_new.escalation_level_id = escalation_level.id
inner join departments on contact_new.departmend_id = departments.id
WHERE loan_no= '$loan_no' if($company_name){ AND company_name= '$company_name'} ")->result();
return $q[0]->num_rows;
}
can i insert the php code as i done in where clause.Is there any other way to do this without using active records.
It's actually very easy:
function search_num_rows($param){
$company_name = (isset($param['company_name']) && !empty($param['company_name']) ? " AND company_name = '$param[company_name]'" : '');
$loan_no=$param['loan_no'];
$q = $this->db->query("select Count(0) as num_rows
from contact_new
inner join companies c on contact_new.company_id = c.id
inner join history on contact_new.id = history.receiver_email
inner join escalation_level on contact_new.escalation_level_id = escalation_level.id
inner join departments on contact_new.departmend_id = departments.id
WHERE loan_no= '$loan_no' $company_name")->result();
return $q[0]->num_rows;
}

Categories