INNER JOIN with multiple tabels - php

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
");

Related

how write sql query with multiple join?

I have fours tables and I wanted to join all three tables with the one table.
I have listed my problem as follows:
Tables:
users
user_training
user_courses
user_certificates
I wanted to get the data from [2,3,4] tables that user_id field matches with the users table ID field.
When I try the INNER JOIN it gives me the result for users that are common in all the tables, But I just wanted to check the [2,3,4] tables with the table [1] Records.
My Query...
SELECT A.training_name AS 'training_name', C.course_name AS 'course_name', D.certificate_name AS 'certificate_name'
FROM user_training AS A INNER JOIN users AS B ON A.user_id=B.ID INNER JOIN user_courses AS C ON B.ID = C.user_id INNER JOIN user_certificates AS D ON B.ID = D.user_id;
Thanks in Advance.
use left join
select u.* from users u
left join user_training ut on ut.user_id=u.user_id
left join user_courses uc on uc.user_id=u.user_id
left join user_certificates uct on uct.user_id=u.user_id
With this one you are getting all users and their respective trainings:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
Changing *_trainig to *_courses or *_certificates will return all users with respected courses or certificates.
If you need to get data in one query, try this one:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
LEFT JOIN `user_courses` ON `users`.`id` = `user_courses`.`user_id`
LEFT JOIN `user_certificates` ON `users`.`id` = `user_certificates`.`user_id`
If user has no trainings, courses, certificates all remaining fields will be null-ed.

MySQL INNER JOIN query issue

I have created an INNER JOIN query as shown below and was wondering how I can make it work? I need for the HomeTeam and AwayTeam to equal TeamID in the query. Any help would be much appreciated. Thanks
$result = mysqli_query($con,"SELECT results.*,team.TeamName
FROM results
INNER JOIN team ON team.TeamID = results.HomeTeam
INNER JOIN team on team.TeamID = results.AwayTeam");
You need to use aliases for the table that you are including twice. Otherwise mysql cannot distinguish between the two.
To be able to process the results easily, you can do the same with the names you are selecting.
Something like:
SELECT
results.*,
t1.TeamName AS TeamNameHome,
t2.TeamName AS TeamNameAway
FROM results
INNER JOIN team t1
ON t1.TeamID = results.HomeTeam
INNER JOIN team t2
ON t2.TeamID = results.AwayTeam

retrieving values from multiple tables in mysql

I am using php and I have to get the data from multiple tables with common id, but the problem is that in few tables that common id contains multiple records,using inner join gives me separate rows of data e.g.
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId":"2"}
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId":"3"}
extraId is the multiple record for the dish_id:52.
I need result like this.
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId"[{"id":"2"},{"id":"3"]}}
My query is:
$orders = "Select DISTINCT order_detail.dish_id,order_detail.quantity,order_detail.STATUS,
order_main.franchise_id,order_main.order_type,
order_extras.extra_id,order_extras.extra_title,
order_addons.addon_id,order_addons.addon_size
from order_main
INNER JOIN order_detail ON order_main.id=order_detail.order_id
INNER JOIN order_extras ON order_main.id=order_extras.order_id
INNER JOIN order_addons ON order_main.id=order_addons.order_id
WHERE order_main.franchise_id='$storeId'
and
order_detail.STATUS!='$order_status'";
please help.
Use group by and group_concat. Something like this:
Select d.dish_id, d.quantity, d.STATUS, m.franchise_id, m.order_type,
group_concat(e.extra_id) as extraids
from order_main m INNER JOIN
order_detail d
ON m.id = d.order_id INNER JOIN
order_extras e
ON m.id = e.order_id INNER JOIN
order_addons a
ON m.id = a.order_id
where m.franchise_id = '$storeId' and d.STATUS <> '$order_status'
group by d.dish_id, d.quantity, d.STATUS, m.franchise_id, m.order_type;
Your desired results do not include these columns:
e.extra_title
a.addon_id
a.addon_size
I would also suggest that you remove the join to order_addons.
Notice that table aliases make the query easier to write and to read.
You can use group bywith dish_id
$orders = "Select DISTINCT order_detail.dish_id,order_detail.quantity,order_detail.STATUS,
order_main.franchise_id,order_main.order_type,
order_extras.extra_id,order_extras.extra_title,
order_addons.addon_id,order_addons.addon_size
from order_main
INNER JOIN order_detail ON order_main.id=order_detail.order_id
INNER JOIN order_extras ON order_main.id=order_extras.order_id
INNER JOIN order_addons ON order_main.id=order_addons.order_id
WHERE order_main.franchise_id='$storeId'
and
order_detail.STATUS!='$order_status' group by order_detail.dish_id";

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.

How I can merge repeated rows in MySQL?

I'm working on a project that each orgnizations have barriers, each organization can contain 1 or more barriers, and then I need generate an XML file that show each barrier into the organization .
But executing this sql code:
SELECT organizations.*, barriers.barrierName AS bname, barriers.type AS btype, geographs.name AS geo, rpd.rpdname AS rpdn, rpd.meaning AS rpdmean FROM organizations
left join orgbarriers on orgbarriers.idOrg = organizations.id
left join barriers on orgbarriers.idBarrier = barriers.id
left join orggeographs on organizations.id = orggeographs.idOrg
left join geographs on geographs.id = orggeographs.idGeo
left join orgrpds on orgrpds.idOrg = organizations.id
left join rpd on rpd.id = orgrpds.idRPD
I get repeated rows like this image:
Use the keyword "DISTINCT"
SELECT DISTINCT rest of your query

Categories