I have this problem with my query. The goal of the query is to display all students, whether they are in the aanweezigheid table or not.
This is my query:
SELECT
s.studentNaam
, s.studentAchterNaam
, s.studentStamNummer
, s.klasID
, k.klasNaam
, k.klasID
, a.studentID
, a.aanwezigTijdAan
, a.aanwezigTijdAf
, a.aanwezigDag
, a.aanwezigStatus
FROM studenten AS s
LEFT JOIN klassen AS k ON s.klasID=k.klasID
LEFT JOIN aanweezigheid AS a ON a.studentID=s.studentID
WHERE k.klasNaam = 'MD2a'
AND a.aanwezigDag='2012-08-28'
ORDER BY s.studentAchterNaam ASC
Any ideas?
Move your WHERE conditions to LEFT JOIN ON clause:
SELECT ...
FROM studenten AS s
LEFT JOIN klassen AS k
ON s.klasID=k.klasID
AND k.klasNaam = 'MD2a'
LEFT JOIN aanweezigheid AS a
ON a.studentID=s.studentID
AND a.aanwezigDag='2012-08-28'
ORDER BY s.studentAchterNaam ASC;
just move your "a" dependency into the ON term. That way you won't be filtering anything out in your WHERE:
SELECT
s.studentNaam
, s.studentAchterNaam
, s.studentStamNummer
, s.klasID
, k.klasNaam
, k.klasID
, a.studentID
, a.aanwezigTijdAan
, a.aanwezigTijdAf
, a.aanwezigDag
, a.aanwezigStatus
FROM studenten AS s
LEFT JOIN klassen AS k ON s.klasID=k.klasID
LEFT JOIN aanweezigheid AS a ON a.studentID=s.studentID
AND a.aanwezigDag='2012-08-28'
WHERE k.klasNaam = 'MD2a'
ORDER BY s.studentAchterNaam ASC
Related
This is my code i am trying to left join the latest team data, not every piece of data. i have tried just using limit 1 but doesnt return anything
ORDER BY updated DESC LIMIT 1
this doesnt work
Any ideas?
$sql = "SELECT
events.id, events.time,events.status, events.home_team,events.away_team,events.league,
ht.id as home_id,ht.name as home_name,at.name as away_name,
statistics.home_goals,statistics.away_goals,statistics.time as game_time,
leagues.id as league_id,leagues.name as league_name,leagues.type as league_type,
country.name as country_name,country.logo,
hts.home_scored, ats.away_scored,
hts.home_conceeded,ats.away_conceeded,
hts.home_win,ats.away_win,
hts.home_15,ats.away_15,
hts.home_25,ats.away_25,
hts.home_btts, ats.away_btts,
hts.home_fts, ats.away_fts,
hts.home_cs, ats.away_cs,
hts.home_corners_for, ats.away_corners_for,
hts.home_corners_against, ats.away_corners_against,
hts.home_cards, ats.away_cards
FROM events
LEFT JOIN teams ht
ON ht.id = events.home_team
LEFT JOIN teams at
ON at.id = events.away_team
LEFT JOIN leagues
ON leagues.id = events.league
LEFT JOIN country
ON country.id=leagues.country
LEFT JOIN ( SELECT team,home_scored,home_conceeded,home_win,home_15,home_25,home_btts,home_fts,home_cs,home_corners_for,home_corners_against,home_cards FROM team_quick_stats ORDER BY updated DESC) hts
ON ht.id=hts.team
LEFT JOIN ( SELECT team,away_scored,away_conceeded,away_win,away_15,away_25,away_btts,away_fts,away_cs,away_corners_for,away_corners_against,away_cards FROM team_quick_stats ORDER BY updated DESC) ats
ON at.id=ats.team
LEFT JOIN statistics
ON statistics.event_id=events.id
WHERE (events.time BETWEEN $start AND $end) ORDER BY country.list_order, leagues.country ASC , leagues.id ASC, events.time ASC, home_name ASC";
Here's one way. Replace LEFT JOIN (SELECT team... etc....) ats with...
LEFT
JOIN
( SELECT x.team
, x.etc...
FROM team_quick_stats x
JOIN
( SELECT team
, MAX(updated) updated
FROM team_quick_stats
GROUP
BY team
) y
ON y.team = x.team
AND y.updated = x.updated
) ats...
Below is my query. I would like to get only 10 posts from post table. However it LIMIT 10 below doesn't limit the results in posts table but it does on another table.
Can anyone help me to fix the query? I really appreicate your help.
SELECT posts.id , posts.cat_id , posts.school_id , posts.campus_id , posts.status , posts.priority , posts.title , posts.content , posts.phone , posts.email , posts.tags , posts.zip , posts.price_new , posts.price_old , posts.reviewed_by , posts.reviewed_date , posts.updated_date , posts.posted_date , posts.expired_date , posts.ip_address , schools.school_name, campuses.campus_name , meta.meta_key , meta_value , images.img_name
FROM posts LEFT JOIN
meta
ON posts.id = meta.post_id LEFT JOIN
images
ON posts.id = images.post_id LEFT JOIN
schools
ON posts.school_id = schools.id LEFT JOIN
campuses
ON posts.campus_id = campuses.id
ORDER BY posts.updated_date DESC LIMIT 10
One method is to use a subquery:
SELECT p.id , p.cat_id, p.school_id, p.campus_id , p.status,
p.priority, p.title, p.content, p.phone, p.email, p.tags, p.zip,
p.price_new, p.price_old, p.reviewed_by, p.reviewed_date,
p.updated_date, p.posted_date, p.expired_date, p.ip_address,
s.school_name, c.campus_name,
m.meta_key, m.meta_value,
i.img_name
FROM (SELECT p.*
FROM posts p
ORDER BY p.updated_date DESC
LIMIT 10
) p LEFT JOIN
meta m
ON p.id = m.post_id LEFT JOIN
images i
ON p.id = i.post_id LEFT JOIN
schools s
ON p.school_id = s.id LEFT JOIN
campuses c
ON p.campus_id = c.id;
Note that the use of table aliases makes the query easier to write, read, and understand.
I have Quiz table which is shown in image.
and quiz result history table like
I write left join query between these two table.
My query is like:
select q.*
, c.name as catname
, qrh.q_complete
, q s.test_type
, qs.questionid
, sum(qrh.score) as score1
, sum(qrh.total_questions) as tot_que
from categories c
, quiz q
left
join user_quiz_results_history qrh
on qrh.quiz_id = q.quizid
left
join quizquestions qq
on qq.quizid = q.quizid
left
join questions qs
on qs.subcatid = qq.subjectid
where c.catid = q.catid
AND q.catid = 1
AND q.status = 'Active'
AND q.enddate >= '2016-05-02'
group
by q.quizid
This query give result all quiz table data and related user_quiz_results table.
If I write query using condition for left join table like userid=90, I don't get the any of data.
I want all data of quiz and if table have result for userid=90 get data from table user_quiz_history.avg(score).
Write condition on left join clause.
select q.* , qrh.q_complete , sum(qrh.score) as score1 , sum(qrh.total_questions) as tot_que from categories c , quiz q left join user_quiz_results_history qrh on qrh.quiz_id = q.quizid AND qrh.userid=84 where q.catid = 1 AND q.status = 'Active' AND q.enddate >= '2016-05-02' group by q.quizid
I am trying to grab the max date from the updates column but also return the corresponding fullname from that table, what is currently happening is that the latest updates.date is being returned but the first updates.consultant is currently being returned as well and we need the correct fullname for the MAX date.
SELECT customer.id,
customer.name,
customer.retainer_value,
customer.customer_type,
clientdetails.performance,
clientdetails.url,
members.fullname AS acc_manager,
u.maxdate,
u.fullname
FROM customer
LEFT JOIN clientdetails
ON clientdetails.id = customer.id
LEFT JOIN members
ON members.id = customer.consultant_name
LEFT JOIN (SELECT updates.clientid,
members.fullname,
Max(updates.`date`) AS MaxDate
FROM updates
LEFT JOIN members
ON members.id = updates.consultant
GROUP BY updates.clientid
ORDER BY updates.date DESC) u
ON customer.id = u.clientid
WHERE customer.switchedoff = 'N'
AND customer.companyid <> '3'
I think the easiest way in your case is to use the substring_index()/group_concat() method:
SELECT customer.id,
customer.name,
customer.retainer_value,
customer.customer_type,
clientdetails.performance,
clientdetails.url,
members.fullname AS acc_manager,
u.maxdate,
u.fullname
FROM customer
LEFT JOIN clientdetails
ON clientdetails.id = customer.id
LEFT JOIN members
ON members.id = customer.consultant_name
LEFT JOIN (SELECT updates.clientid,
substring_index(group_concat(m.fullname order by u.date desc separator '|'), '|', 1) as full_name
Max(updates.`date`) AS MaxDate
FROM updates u
LEFT JOIN members m
ON m.id = u.consultant
GROUP BY u.clientid
) u
ON customer.id = u.clientid
WHERE customer.switchedoff = 'N'
AND customer.companyid <> '3' ;
i have two table i joining both tables and want display extreme values of select one columns this is php/mysql code.
$sql="Select A.date, A.rainfall, A.evep ,
A.max_temp , A.min_temp , A.sunshine_hrs ,
B.temp_air , B.dry_temp ,B.wet_temp,
B.rel_humid , B.soil_temp_5, B.soil_temp_20,
B.soil_temp_30 , B.soil_temp_60 ,
B.air_pressure
FROM reg_data3 A
INNER JOIN reg_data2 B
ON A.date = B.date
WHERE year(DATE_FORMAT(A.date, '%y-%m-%d'))='".$year."'
and month(DATE_FORMAT(A.date, '%y-%m-%d'))='".$month."'
ORDER BY B.date and $paramiter > $value ";
when echo query; first i select $year=2008 $month=2 and $parameter=soil_temp_60 and $value=20
Select A.date, A.rainfall, A.evep , A.max_temp ,
A.min_temp , A.sunshine_hrs , B.temp_air ,
B.dry_temp ,B.wet_temp, B.rel_humid ,
B.soil_temp_5 ,B.soil_temp_20 , B.soil_temp_30 ,
B.soil_temp_60 , B.air_pressure
FROM reg_data3 A
INNER JOIN reg_data2 B
ON A.date = B.date
WHERE year(DATE_FORMAT(A.date, '%y-%m-%d'))='2008'
and month(DATE_FORMAT(A.date, '%y-%m-%d'))='02'
ORDER BY B.date and A.soil_temp_60 > 24 ;
extreme values in table related to this query but not work well
I think that your problem is with variables in wrong place, try this:
$sql="Select A.date, A.rainfall, A.evep ,
A.max_temp , A.min_temp , A.sunshine_hrs ,
B.temp_air , B.dry_temp ,B.wet_temp,
B.rel_humid , B.soil_temp_5, B.soil_temp_20,
B.soil_temp_30 , B.soil_temp_60 ,
B.air_pressure
FROM reg_data3 A
INNER JOIN reg_data2 B
ON A.date = B.date
WHERE year(DATE_FORMAT(A.date, '%y-%m-%d'))='".$year."'
and month(DATE_FORMAT(A.date, '%y-%m-%d'))='".$month."'
and $paramiter > $value
ORDER BY B.date ";