dynamically joining the tables on queries slow performance - php

I want to show the data of more than 50 tables in one list view. I have formed the query below.
Left join is dynamically added using PHP forloop.
Any other ways to get values from various tables(more than 50 tables)?
Or please suggest any other technology to use PHP Product...
SELECT * FROM main_table `t`
LEFT JOIN `table` ON table.c_application_id = t.id
LEFT JOIN `table1` ON table1.c_application_id = t.id
LEFT JOIN `table2` ON table2.c_application_id = t.id
LEFT JOIN `table3` ON table3.c_application_id = t.id
LEFT JOIN `table4` ON table4.c_application_id = t.id
LEFT JOIN `table5` ON table5.c_application_id = t.id
LEFT JOIN `table6` ON table6.c_application_id = t.id
LEFT JOIN `table7` ON table7.c_application_id = t.id
LEFT JOIN `table8` ON table8.c_application_id = t.id
LEFT JOIN `table9` ON table9.c_application_id = t.id
LEFT JOIN `table10` ON table10.c_application_id = t.id
LEFT JOIN `table11` ON table11.c_application_id = t.id
LEFT JOIN `table12` ON table12.id=t.c_cycle
LEFT JOIN `table13` ON table13.c_groupId=t.c_program_to_apply
LEFT JOIN `table14` ON table14.id = table14.c_status_id
LEFT JOIN `table15` ON table15.id = table15.c_stage_id
LEFT JOIN `table16` ON table16.c_code = table16.c_country
LEFT JOIN `table17` ON table17.id=table17.c_app_form

Related

I'm not able to convert the Mysql query into laravel query builder

I'm struggling to convert this below MySQL query into laravel query builder. It has a lot of joins and subqueries inside. Can someone please help me?
MYSQL query:
SELECT a.id , b.*, a.agent_id, td.contacts, td.addresses
FROM teams a
left join team_details as td on td.team_id = a.id
LEFT OUTER JOIN
(
SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to
) b ON a.id = b.team_id
order by a.id
I'm trying something like below but got stuck with LEFT OUTER JOIN as it has sub query inside :).
DB::table('teams as a')
->leftjoin('team_details as td', 'td.team_id','=','a.id')
->select('a.id', 'b.*', 'a.agent_id','td.contacts','td.addresses')
->leftouterjoin(DB::select(DB::raw('SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to')))
->get();
Any suggestions, please? I want to apply paginate function paginate() instead of get() for this query builder.
I think you should try.
$cards = DB::select("SELECT a.id , b.*, a.agent_id, td.contacts, td.addresses
FROM teams a
left join team_details as td on td.team_id = a.id
LEFT OUTER JOIN
(
SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to
) b ON a.id = b.team_id
order by a.id");
Hope it help you.

Left join not working in my prepared statement (mysqli)

I can't get a left join to work in my prepared statement.
"SELECT DISTINCT(a.auto_id), m.merk, a.model, a.uitvoering, a.standaardtekst, a.prijs, a.prijs2, a.prijs3, a.handelsprijs, a.aanmaak, s.soort, z.prijs_id
/*,GROUP_CONCAT(DISTINCT(apc.NL) ORDER BY apc.NL ASC)*/
FROM autocom_new.auto_new a
INNER JOIN autocom_new.tbl_merken m
ON a.merk = m.merk_id
INNER JOIN autocom_new.tbl_soort s
ON a.soort = s.soort_id
INNER JOIN autocom_new.auto_zoekmachines z
ON a.auto_id = z.auto_id
/*
LEFT JOIN autocom_new.auto_accessoire acc
ON a.auto_id = acc.auto_id
LEFT JOIN autocom_new.tbl_autopricecode_new apc
ON acc.code_id = apc.code_id
*/
WHERE a.ac LIKE ? AND a.flag = ?"
The commented parts are the parts that aren't working.
I have no idea what I'm doing wrong.
EDIT
First of all I forgot that both tables have a column ac, so I've changed the where statement a bit. The left joins are working now, but the part in the select is still not working
So the problem was that I forgot a GROUP BY.
"SELECT DISTINCT(a.auto_id), m.merk, a.model, a.uitvoering, a.standaardtekst, a.prijs, a.prijs2, a.prijs3, a.handelsprijs, a.aanmaak, s.soort, z.prijs_id,
GROUP_CONCAT(DISTINCT(apc.NL) ORDER BY apc.NL ASC)
FROM autocom_new.auto_new a
INNER JOIN autocom_new.tbl_merken m
ON a.merk = m.merk_id
INNER JOIN autocom_new.tbl_soort s
ON a.soort = s.soort_id
INNER JOIN autocom_new.auto_zoekmachines z
ON a.auto_id = z.auto_id
LEFT JOIN autocom_new.auto_accessoire acc
ON a.auto_id = acc.auto_id
LEFT JOIN autocom_new.tbl_autopricecode_new apc
ON acc.code_id = apc.code_id
WHERE a.ac LIKE ? AND a.flag = ?
GROUP BY a.auto_id"

Attempting to add count function to a complex query PHP MYSQL

I have a complex query consisting of lots of Inner and Left Joins and i am attempting to get the number of record back in a certain table using the COUNT Function of SQL.
I am attempting to get the number of records in the 'held_proposals' table for each proposal using the proposal_id since I'll be outputting the data into a table using a foreach loop.
In essence I would like to show how many students have 'held' a proposal against each proposal.
This was my attempt:
SELECT p.proposal_id, p.proposal_title, p.description, u.user_record_id, u.forename, u.surname, c.course_title, h.*,
GROUP_CONCAT(DISTINCT t.tag_title) AS tags FROM proposal p
LEFT JOIN user u on u.user_record_id = p.user_record_id
LEFT JOIN course_details c on c.course_code = p.course_code
LEFT JOIN record r on r.proposal_id = p.proposal_id
LEFT JOIN proposal_tags pt on pt.proposal_id = p.proposal_id
LEFT JOIN tag_details t on t.tag_code = pt.tag_code
LEFT JOIN (
SELECT h.student_record_id, COUNT(*) AS Held
FROM held_proposals h
) H on h.proposal_id = p.proposal_id
WHERE p.source = "Supervisor"
AND (r.status_code not in (3,8) OR r.status_code IS NULL)
GROUP BY p.proposal_id;
the table currently looks like this:
I would like to add the value returned from the 'held_proposals' table for each proposal at the end of the table as another column.
Could any please provide me with some guidance as to how I can achieve this in the SQL query. Thank you in advance.
You need to group the count inside the query which you are joining something as
SELECT
p.proposal_id,
p.proposal_title,
p.description,
u.user_record_id,
u.forename,
u.surname,
c.course_title, coalesce(h.Held,0) as `Held`,
GROUP_CONCAT(DISTINCT t.tag_title) AS tags
FROM proposal p
LEFT JOIN user u on u.user_record_id = p.user_record_id
LEFT JOIN course_details c on c.course_code = p.course_code
LEFT JOIN record r on r.proposal_id = p.proposal_id
LEFT JOIN proposal_tags pt on pt.proposal_id = p.proposal_id
LEFT JOIN tag_details t on t.tag_code = pt.tag_code
LEFT JOIN (
SELECT proposal_id, COUNT(*) AS Held
FROM held_proposals group by proposal_id
) h on h.proposal_id = p.proposal_id
WHERE p.source = "Supervisor"
AND (r.status_code not in (3,8) OR r.status_code IS NULL)
GROUP BY p.proposal_id;

Using multiple tables inner join on one key item. Can't figure out what is wrong

For the life of me I can not figure out where I have went wrong
I am pulling the data from these multiple tables but no data appearing
$result=mysql_query("SELECT * FROM chars uc
INNER JOIN zone_settings t ON uc.pos_zone = t.zoneid
INNER JOIN char_look v ON uc.charid = v.charid
INNER JOIN char_jobs y ON uc.charid = y.charid
INNER JOIN char_stats n ON uc.charid = n.charid
INNER JOIN char_profile p ON uc.charid = p.charid
WHERE `accid`='".$user["id"]."' ORDER BY `charid`");
Thanks kwolfe using LEFT JOIN and Removing the ORDER BY it works now. Here is the code.
$result=mysql_query("SELECT * FROM chars uc
LEFT JOIN zone_settings t ON uc.pos_zone = t.zoneid
LEFT JOIN char_look v ON uc.charid = v.charid
LEFT JOIN char_jobs y ON uc.charid = y.charid
LEFT JOIN char_stats n ON uc.charid = n.charid
LEFT JOIN char_profile p ON uc.charid = p.charid
WHERE `accid`='".$user["id"]."'");
Switch to LEFT JOINS to see if your missing a relationship along the way (INNER JOIN will only show data where a relationship is made for each table, in this case ALL tables.)

MYSQL LEFT JOIN Multiple Tables Twice

I have following MySQL Code:
SELECT *
FROM b_movies, b_mov_rol_celeb, b_starcast
LEFT JOIN bb_celebs ON b_mov_rol_celeb.celeb_id = bb_celebs.celeb_id
OR b_starcast.celeb_id = bb_celebs.celeb_id
LEFT JOIN bb_roles ON b_mov_rol_celeb.role_id = bb_roles.role_id
WHERE b_movies.id = '14';
Its giving error:
Unknown column 'b_mov_rol_celeb.celeb_id' in 'on clause'
Try this:
SELECT m.title_slug, m.title, m.release_date,
c.celeb_slug, c.celeb_name,
r.role_name,
s.char_name
FROM b_movies m
INNER JOIN b_starcast s ON m.id = s.movie_id
INNER JOIN b_mov_rol_celeb mrc ON m.id = mrc.movie_id
LEFT JOIN bb_celebs c ON mrc.celeb_id = c.celeb_id OR s.celeb_id = c.celeb_id
LEFT JOIN bb_roles r ON mrc.role_id = r.role_id
WHERE m.id = '14';
The order of the tables matters in a left or right join.

Categories