I am having an issue with my sql, I am trying to display a user based of their age group, attaching the string along with the primary key, but i only want that one value to be shown
I believe it is something like this
SELECT users.gbpfid,
users.aid,
compresults.total,
agegroup.aid AS aid2
FROM compresults
INNER JOIN competitions
ON competitions.cid = compresults.cid
INNER JOIN agegroup
ON agegroup.aid = users.aid
INNER JOIN users
ON compresults.gbpfid = users.gbpfid
WHERE competitions.compdate = (SELECT competitions.compdate
FROM competitions
INNER JOIN compresults
ON compresults.cid =
competitions.cid
WHERE compresults.gbpfid = users.gbpfid
ORDER BY competitions.compdate DESC
LIMIT 1)
however this throws up this error
SQL Error (1054): Unknown column 'users.aid' in 'on clause'
Which i cannot make any sense
when i remove the
"on agegroup.aid = users.aid" from line 5
it displays the records, but for each aid
I am confused how it recognises the column without the inner join (specifying the connection) but when i do whole inner join of the agegroup table, it joins fine, but with all the records
any ideas? thanks
You are trying to join agegroups using users.aid before users table is even joined.
INNER JOIN competitions
ON competitions.cid = compresults.cid
INNER JOIN agegroup
ON agegroup.aid = users.aid
INNER JOIN users
ON compresults.gbpfid = users.gbpfid
As you can see you are truing to access users.aid before is joined. Change order of the joins to:
competitions -> users -> agegroup
This should work:
INNER JOIN competitions
ON competitions.cid = compresults.cid
INNER JOIN users
ON compresults.gbpfid = users.gbpfid
INNER JOIN agegroup
ON agegroup.aid = users.aid
You should be able to access users.aid now.
Hope this helps.
The cause of the isue is that joins are processed from left to right. In your code you have the following order:
INNER JOIN agegroup
ON agegroup.aid = users.aid
INNER JOIN users
So, in the ON agegroup.aid = users.aid you reference the users table before it is joined to the fray.
Change the order of the joins:
compresults -> competitions -> users -> agegroup
Related
I have two tables (user and I_S) and the I_S table contains a column called score. I need to obtain the highest score and the corresponding user name from the tables.
I used a left join but I keep getting an error saying
Unknown column 'I_S.total_score' in 'field list'
This is the query I used.
SELECT MAX(interview_sessions.total_score)
FROM interview_sessions as sc
LEFT JOIN user as u on sc.user_id = u.user_id
SELECT DISTINCT users.name, I_S.score FROM users
LEFT JOIN I_S ON I_S.user_id = users.user_id WHERE score IN (SELECT MAX(score) FROM I_S)
All users with Max score.
And i think you should write sc.total_score instead interview_sessions.total_score in your query because you use alias
Try using this:
SELECT MAX(sc.total_score)
FROM interview_sessions as sc
LEFT JOIN user as u on sc.user_id = u.user_id
You need an inner query to first determine WHAT is the highest score. Form that, re-join to the interview sessions table on THAT max score. Then join to users to get the names. It is possible multiple people can have a same max score.
select
from
( SELECT MAX(i_s.total_score) as MaxScore
from interview_sessions i_s ) maxAll
JOIN interview_sessions i_s2
on maxAll.MaxScore = i_s2.total_score
JOIN User u
on i_s2.user_id = u.user_id
try this way
select b.*,a.totalscore from user b
inner join( select userid,max(total_score) as totalscore from I_S) a on a.userid=b.userid
I am trying to display the driver's first and last name but when I run this query the name columns just return test and not the actual names from the employee table
SELECT checklistitem.*,
m1.Company_ID AS Company_ID,
m1.ChecklistID As ChecklistID,
e.FirstName As FirstName,
e.LastName As LastName
FROM checklistitem
LEFT JOIN employee e ON e.CompanyID
LEFT JOIN maintenance m1 ON m1.CheckListID
LEFT JOIN Vehicle v ON m1.LinkedID = v.ID
LEFT JOIN Trailer t ON m1.LinkedID = t.ID
WHERE m1.Company_ID = 129
I thought maybe the table wasn't linking correctly to find the names so I tried changing the code to LEFT JOIN employee e ON m1.Company_ID because both the maintenance and the employee table have a company ID but I get the error
Unknown column 'm1.Company_ID' in 'on clause'
Your issue is with the LEFT JOIN statements. You have an invalid syntax in your ON clause.
FROM checklistitem
LEFT JOIN employee e ON e.CompanyID /*ERROR*/
LEFT JOIN maintenance m1 ON m1.CheckListID /*ERROR*/
Your SQL statement has to be table.column = table2.column
So I'm gonna go out on a guess and say you should being using
FROM checklistitem
LEFT JOIN employee e ON checklistitem.CompanyID = e.CompanyID
LEFT JOIN maintenance m1 ON e.CheckListID = m1.CheckListID
Please note: I don't know what your tables are setup like. checklistitem.CompanyID and e.CheckListID was a guess. Please replace those with the correct fields from your tables.
Also with your table names, m1.Company_ID will automatically turn into Company_ID. No need to use AS.
My query is only showing a subset of records of people who have messages for me whereas I want to return a list of all users wether they have messages for me or not along with the count (or true/false, 0,1)
SELECT attendee.id, attendee.firstName, attendee.lastName, count(attendee_chat.to)
from attendee_chat
INNER JOIN attendee ON attendee.id = attendee_chat.to
INNER JOIN chat ON attendee_chat.id = chat.attendee_chat_id
WHERE attendee.id <> 1
GROUP BY attendee_chat.to;
picture is worth a thousand words.
Put the attendee table first in the FROM, and then left join to the other tables that may not have records.
SELECT attendee.id, attendee.firstName, attendee.lastName, count(attendee_chat.to)
from attendee
LEFT JOIN attendee_chat ON attendee_chat.to = attendee.id
LEFT JOIN chat ON attendee_chat.id = chat.attendee_chat_id
WHERE attendee.id <> 1
GROUP BY attendee_chat.to;
I would avoid doing a FULL OUTER JOIN as it is not necessary and may lead to some unexpected results.
EDIT: Try this. Remove the chat table as you aren't selecting from it anyway, and group on the fields from the primary table attendee, rather than the on tables that may not contain any data.
SELECT attendee.id, attendee.firstName, attendee.lastName, count(attendee_chat.to)
from attendee
LEFT JOIN attendee_chat ON attendee_chat.to = attendee.id
WHERE attendee.id <> 1
GROUP BY attendee.id, attendee.firstName, attendee.lastName;
EDIT2: After discussion of requirements that he wants a count of chats that were to him (#1)
SELECT attendee.id, attendee.firstName, attendee.lastName,
(SELECT count(1) FROM attendee_chat
WHERE attendee_chat.from = attendee.id and attendee_chat.to = 1) as chatcount
from attendee ;
EDIT3: After comment about external ID
SELECT attendee.id, attendee.firstName, attendee.lastName, attendee.attendee_id,
(SELECT count(1) FROM attendee_chat
INNER JOIN attendee a2 ON a2.id = attendee_chat.toid
WHERE attendee_chat.fromid = attendee.id and a2.attendee_id = 123
) as chatcount
FROM attendee ;
P.S. I've changed from to fromid and to -> toid as it is dangerous using reserved words as object names ;-) SQL Fiddle here
If you want to do a join on records even if they have no matching element(s) in the other relation, you probably want an OUTER JOIN instead of an INNER JOIN.
MySQL: Quick breakdown of the types of joins
I currently have 5 tables in MySQL database. Some of them share foreign keys and are interdependent of each other. I am trying to create a query that will show all the results side by side(major, course, semester, etc). The query I created query it is not displaying my desired results since I have not added the other tables. I am not sure how to implement the other tables. How can I modify the mysql-query to display all the results in order?
Query
select * from course left join major on course.id = majors.id left join majors on courses_major_xref.majors_id = majors.id
Try the following
SELECT * FROM course
INNER JOIN major_courses_xref ON course.id = major_courses_xref.course_id
INNER join majors ON major_courses_xref.majors_id = majors.id
INNER JOIN courses_semester_xref ON course.id = courses_semester_xref.course_id
INNER JOIN semester ON courses_semester_xref.semester_id = semester.id;
I think there is just some order of operations problems in your query, try:
SELECT * from course
LEFT JOIN major_course_xref
ON course.id = major_course_xref.courseID
LEFT JOIN major
ON major.id = major_course_xref.major_id
LEFT JOIN course_semester_xref
ON course.id = course_semester_xref.course_id
LEFT JOIN semester
ON course_semester_xref.semester_id = semester.id
Here is the schema of the database I'm working with - [redacted]
I'm trying to come up with 2 different queries for 2 different result sets -
Sales representatives with the highest sales
Sales representatives with the highest sales grouped by managerId
I've had some luck with the 1st query; this is what I came up with:
SELECT
SUM(`products`.`cost`) AS `Sale`
, `employees`.`firstName`
FROM
`d2dpro`.`sales_reps`
, `d2dpro`.`products`
INNER JOIN `d2dpro`.`employees`
ON (`sales_reps`.`employeeId` = `employees`.`employeeId`)
INNER JOIN `d2dpro`.`sold_products`
ON (`products`.`productId` = `sold_products`.`productId`)
INNER JOIN `d2dpro`.`sales`
ON (`sold_products`.`saleId` = `sales`.`saleId`) AND (`sales`.`salesCampId` = `sales_reps`.`saleCampId`)
GROUP BY `employees`.`firstName`;
With this query, I'm stuck with this error:
Error Code: 1054
Unknown column 'sales_reps.saleCampId' in 'on clause'
Any help with this query? And also for the 2nd one?
You are using one table (sales_reps) that is CROSS JOIN-ed with the INNER JOIN of the tables employees, sold_products and sales. To be able to reference sales_reps in that JOIN you should include it as inner join: (the FROM clause should look like this)
FROM `d2dpro`.`products`
INNER JOIN `d2dpro`.`employees`
ON (`sales_reps`.`employeeId` = `employees`.`employeeId`)
INNER JOIN `d2dpro`.`sold_products`
ON (`products`.`productId` = `sold_products`.`productId`)
INNER JOIN `d2dpro`.`sales`
ON (`sold_products`.`saleId` = `sales`.`saleId`)
INNER JOIN `d2dpro`.`sales_reps`
ON (`sales`.`salesCampId` = `sales_reps`.`saleCampId`)