I am sorry, I know there is a lot of answer on the forum concerning that question but I didn't succeed to adapt an answer to my problem.
Here is my function :
public function getPupil($db, $pupil_id){
$pupil = $db->query('
SELECT
a.gender as genderPupil,
pupil_name,
pupil_lastname,
a.id as pupilId,
b.id as idOne,
c.id as idTwo,
b.lastname as lastnameOne,
b.name as nameOne,
c.name as nameTwo,
c.lastname as lastnameTwo,
birthdate,
school,
level,
allergies,
special_diet,
tap,
d.name as tapNameFirstTerm,
e.name as tapNameSecondTerm,
f.name as tapNameThirdTerm
FROM pupils a
INNER JOIN responsibles b ON a.id_responsibles = b.id
INNER JOIN responsibles c ON a.id_responsibles_two = c.id
INNER JOIN tap_activities d ON a.tap_first_term = d.id
INNER JOIN tap_activities e ON a.tap_second_term = e.id
INNER JOIN tap_activities f ON a.tap_third_term = f.id
WHERE a.id = ?
', [$pupil_id])->fetch();
return $pupil;
}
I would like to add conditions to do not query the INNER JOIN when a.tap_first_term or a.tap_second_term or a.tap_third_term are equal to 0.
Thank you very much for your answers.
I found my solution.
For the people that could be interested, I just changed the three INNER JOIN for the tap_activities by LEFT JOIN. And it works as I want.
Related
i want to translate this code in phpmyadmin to laravel
SELECT AVG(valeur_5), id_client, nom_propriete, prenom_propreite, nom_entreprise, type_utilisatuer
FROM cliets c
INNER JOIN evaluations e on c.id = e.id_client
INNER JOIN users u on u.id = c.id_user
INNER JOIN propreites p on p.id_user = u.id
GROUP BY id_client
If you want to embed raw SQL into your program, just wrap it in a DB::select.
DB::select("SELECT AVG(valeur_5), id_client, nom_propriete, prenom_propreite, nom_entreprise, type_utilisatuer FROM cliets c INNER JOIN evaluations e on c.id = e.id_client INNER JOIN users u on u.id = c.id_user INNER JOIN propreites p on p.id_user = u.id GROUP BY id_client")
Alternatively, you can rewrite it with the Eloquent query builder.
try this:
$orders = DB::table('hrayfis')
->join('evaluations','evaluations.id_herfi','hrayfis.id')
->join('users','users.id','hrayfis.id_user')
->join('propreites','propreites.id_user','users.id')
->select(
DB::raw('avg(valeur_5)','propreites.nom_propriete '))
->groupBy('hrayfis.id')->get();
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.
Hello I added GROUP_CONCAT function to my query and that function killed my query :/.
My query is :
SELECT u.username,a.user_id,a.id,a.text,a.lang as fromLang,b.lang as toLang,GROUP_CONCAT(DISTINCT b.id) AS translation_ids FROM sentence as a
INNER JOIN sentence_relationship as sr ON
(sr.sentence_id = a.id)
INNER JOIN sentence as b ON
(b.id = sr.translation_id AND a.id = sr.sentence_id)
INNER JOIN users as u ON
(u.id = a.user_id) GROUP BY a.id LIMIT 10;
What is wrong with that query ?
This is your query (somewhat formatted):
SELECT u.username, a.user_id, a.id,a.text,a.lang as fromLang, b.lang as toLang,
GROUP_CONCAT(DISTINCT b.id) AS translation_ids
FROM sentence a INNER JOIN
sentence_relationship sr
ON sr.sentence_id = a.id INNER JOIN
sentence b
ON b.id = sr.translation_id AND a.id = sr.sentence_id INNER JOIN
users as u
ON u.id = a.user_id
GROUP BY a.id
LIMIT 10;
It is unclear from your question whether the group_concat() was added with the group by. That could slow things down.
The limit 10 is taking the first 10 a.ids that match (the group by does an implicit ordering). If you do this with a subquery, it will probably speed up the query:
SELECT u.username, a.user_id, a.id,a.text,a.lang as fromLang, b.lang as toLang,
GROUP_CONCAT(DISTINCT b.id) AS translation_ids
FROM (select s.*
from sentence s
order by a.id
limit 10
) a INNER JOIN
sentence_relationship sr
ON sr.sentence_id = a.id INNER JOIN
sentence b
ON b.id = sr.translation_id AND a.id = sr.sentence_id INNER JOIN
users as u
ON u.id = a.user_id
GROUP BY a.id;
This assumes that all the joins do work and match records. If the joins are used for filtering, then you may get fewer than 10 rows back.
I'm a little confused in here and need some help...
the situation is I've made three tables(fr_Leagues, fr_nations and fr_confeds), all i want to do is add a league which shows the name of the categories not the i.d with pagination. Here is the code:
NOW FIXED!
"SELECT
a.id as confed_id,
a.fr_short_name as confed_name,
b.id as nation_id,
b.fr_name as nation_name,
c.id as league_id,
c.fr_name as league_name"
." FROM fr_confeds as a
INNER JOIN fr_nations as b ON a.id = b.confed_id
INNER JOIN fr_leagues as c ON b.id = c.nation_id"
." LIMIT $paginate->start, $paginate->limit"
You are missing on how to link the different tables together. On each INNER JOIN, you need to have it:
INNER JOIN fr_nations ON a.<someColumn> = b.<anotherColumn> INNER JOIN fr_leagues ON a.<someColumn> = b.<anotherColumn>
USE THIS QUERY
SELECT * FROM fr_confeds as A
INNER JOIN fr_nations as B ON A.id = B.confed_id
INNER JOIN fr_leagues as C ON B.confed_id = C.league_id
LIMIT $paginate->start, $paginate->limit
This code only shows categories with articles in them.
I want to show all categories.
Pls help.
$query = "SELECT C.id, C.jcat_name,COUNT(A.catid) AS catid FROM jt_categories C INNER JOIN jt_articles A ON C.id = A.catid GROUP BY C.id";
change to left join
SELECT C.id, C.jcat_name,COUNT(A.catid) AS catid FROM jt_categories C
LEFT JOIN jt_articles A ON C.id = A.catid GROUP BY C.id
Change INNER JOIN for LEFT JOIN in your query.
INNER JOIN looks explicitely for the join in the data
replace the inner join by a left outer join
Change INNER JOIN to LEFT JOIN.
Did u try LEFT JOIN ? bc. (i think) in second table u have NULL articles for some categories.