SELECT ID, Name
FROM Members
WHERE ID IN
(SELECT Member ID
FROM Team Member
WHERE Team ID IN
(SELECT ID
FROM Teams
WHERE Year = 2012 AND Country = 'Phillipines'))
When I input this query into phpmyadmin, I get error #1064 saying I do not have the correct syntax. How can resolve this?
The immediate error appears to be WHERE Team ID IN ... which should actually be WHERE TeamID IN .... There are also other similar errors. But, we can try rewriting your query using joins, so that it is more readable and maintainable:
SELECT m.ID, m.Name
FROM Members m
INNER JOIN TeamMember tm
ON m.ID = tm.MemberID
INNER JOIN Teams t
ON tm.TeamID = t.ID
WHERE
t.Year = 2012 AND t.Country = 'Phillipines';
In general, identifiers in SQL have to be a single word without any whitespace. If Team ID is actually a column name, then you would have to refer to it in MySQL using backticks. But, you should avoid doing this.
Your table Team Member and its columns Member ID and Team ID maybe mispelled.
I think you are using wrong variable name (Member ID) it should not have space in between Member and Id. in the same way (Team Member), (Team ID).
Please make sure you use the right name for column name and table name.
You can try below using join
SELECT a.ID, a.Name
FROM Members a inner join Team b on a.ID=b.MemberID
inner join Teams c on b.TeamID=c.ID
where c.Year = 2012 AND c.Country = 'Phillipines'
Related
I’m studying sql command line, and using command in mysql via terminal. I’m studying INNER JOIN, and I found it easy enough, but I have a doubt that I must solve otherwise cannot manage to pass to other commands.
I have these four tables: departments, employees, employees_projects, projects
These tables have the following content:
departments:
id int
name varchar(60)
employees:
id int
first_name varchar(60)
last_name varchar(60)
salary int
department_id int
employees_projects:
project_id int
employee_id int
projects:
id int
title varchar(60)
start_date date
end_date date
budget int
When I want to use INNER JOIN to join two tables I simply use it this way:
SELECT employees.first_name, employees.last_name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
When I want to join three tables I use the following:
SELECT employees.first_name, employees.last_name, departments.name, employees_projects.project_id
FROM ( (employees
INNER JOIN departments ON employees.department_id = departments.id)
INNER JOIN employees_projects ON employees.id = employees_projects.employee_id);
Here is my question: As you can see the result of the last line will give me employees.first_name, employees.last_name, departments.name, employees_projects.project_id, this mean I would get the employee’s name, surname, department and id of the project they are working on. Now, considering the tables we have above, I cannot manage to create a one line SQL query that give me as a result employees.first_name, employees.last_name, departments.name, projects.title. This mean one query that would give me employee’s name, surname, department and the project’s name they are working on.
Is it possible to achieve this in one line query?
mysql> Thank you everybody!
EDITING THE POST:
I created the following line:
SELECT employees.first_name, employees.last_name, departments.name, projects.title FROM ( (employees, projects INNER JOIN departments ON employees.department_id = departments.id) INNER JOIN employees_projects ON projects.id = employees_projects.project_id AND employees_projects.employee_id = employees.id);
But unfortunately I'm receiving this error, and it's strange because looks the correct column:
mysql> SELECT employees.first_name, employees.last_name, departments.name, projects.title FROM ( (employees, projects INNER JOIN departments ON employees.department_id = departments.id) INNER JOIN employees_projects ON projects.id = employees_projects.project_id AND employees_projects.employee_id = employees.id);
ERROR 1054 (42S22): Unknown column 'employees.department_id' in 'on clause'
mysql>
You are probably familiar with arithmetic notation. You can make an arithmetic expression with any number of terms:
a + b + c + d
The first term has nothing before it. Each term has a + between it. There really isn't a "main" term, because addition has the algebraic property of commutativity. The expression has the same result as:
c + a + d + b
Or any other ordering of the terms.
In SQL, joins are similar. You can in theory join any number of tables.
<table> INNER JOIN <table> ON <condition>
INNER JOIN <table> ON <condition>
INNER JOIN <table> ON <condition>
And you can keep adding more after that if you need to. (I said in theory because MySQL or any other SQL product is just one implementation of the language and it may have a practical limit. In MySQL's case, the limit is 63 tables per query but that's not the fault of the language, it's just how MySQL's code implements joins.)
FROM is not part of the expression, and it doesn't name any table as the "main" table. Inner join has the property of commutativity, like addition in arithmetic. In fact, MySQL can reorder the tables itself as it runs your query, if it turns out to make the query have better performance.
You tried this join:
FROM ( (employees, projects INNER JOIN departments
ON employees.department_id = departments.id)
INNER JOIN employees_projects
ON projects.id = employees_projects.project_id
AND employees_projects.employee_id = employees.id);
You're mixing two syntax forms, and it causes a problem.
Joins with comma are from 1989, and these still work. But they couldn't do OUTER JOIN and so the syntax was changed in 1992 to have the INNER/OUTER JOIN keywords.
But the problem is that the syntax with the INNER/OUTER JOIN keywords has higher precedence than comma. So it's as if your query were joining projects and departments before the query engine has even acknowledged that employees is part of the query. When it checks the ON condition that references employees, it doesn't know about that table.
This is in fact documented: https://dev.mysql.com/doc/refman/8.0/en/join.html search for "JOIN has higher precedence than the comma operator" near the bottom of the page.
The solution is to use only the 1992 style syntax consistently. I would write it the way #Adamszsz writes the query in the other answer.
Use need something like this :
select emp.first_name,emp.last_name,d.name,p.title , p.id from employees emp
join departments d on d.id = emp.department_id
join employees_projects empp on empp.employee_id = emp.id
join projects p on p.id = empp.project_id
When trying to run I get the error "Not unique table/alias: 'Caller'", im not sure what it means but i feel like it has something to do with joining the same two tables twice on different values
SELECT Company_name,Contact_id, COUNT(Company_name) as nc
FROM Customer
JOIN Caller ON Customer.Company_ref = Caller.Company_ref
JOIN Caller ON Customer.Contact_id = Caller.Caller_id
JOIN Issue ON Caller.Caller_id = Issue.Caller_id
GROUP by Company_name, Contact_id
HAVING COUNT(Company_name) < 5
if you are using the same table call it another name or both tables to have different names... in this case to avoid confusion let A and B.
SELECT Company_name,Contact_id, COUNT(Company_name) as nc
FROM Customer
JOIN Caller A ON Customer.Company_ref = A.Company_ref
JOIN Caller B ON Customer.Contact_id = B.Caller_id
JOIN Issue ON A.Caller_id = B.Caller_id
GROUP by Company_name, Contact_id
HAVING COUNT(Company_name) < 5
You need to qualify your column names. I can guess:
SELECT c.Company_name, c.Contact_id, COUNT(*) as nc
FROM Customer JOIN
Caller co
ON co.Company_ref = c.Company_ref JOIN
Caller ca
ON c.Contact_id = ca.Caller_id JOIN
Issue i
ON ca.Caller_id = i.Caller_id
GROUP by c.Company_name, c.Contact_id
HAVING COUNT(*) < 5;
Whenever you have multiple tables in a query, you should use table aliases and qualified table names.
Without seeing the data, I'm not sure if this does anything useful. The two joins to the same table on different keys are suspicious.
So I have the below database structure
TABLES ------- Columns
person: id, name, salary, address
group: id, name
person_group: person_id, groud_id
So here is my query which is used to get all persons along with the groups they are associated with
SELECT p.id, p.name,
group_concat(g.name) as groups
FROM person_group pg, group g, person p
WHERE pg.group_id = g.id AND pg.novel_id = n.id
GROUP BY ng.person_id
So this query gives me data like
id name groups
2345 John Admin, SuperAdmin, RedHat
But the problem is: if that person doesn't belong to any group, the query doesn't return that person!
Any would be appreciated!
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
That is exactly your problem here. When you are writing more than one table in the FROM clause, you should be thinking "what type of JOIN do I need". If you had that thought, you would immediate realize that you need an outer join to do what you want:
SELECT p.id, p.name, group_concat(g.name) as groups
FROM person p LEFT JOIN
person_group pg
ON pg.person_id = p.id LEFT JOIN
group g
ON pg.group_id = g.id
GROUP BY p.id, p.name;
If you don't know what an outer join is, then that is all the more reason to use the proper, explicit syntax, so you can learn.
I have four table like the picture bellow
I want to count how many student that have status 'v' where in table submission have submission type '1' and group by student_id so in the last i can get table like this
I have try sql query like this
select p.id, (SELECT count(*) FROM (select b.id from student as a , submission as b WHERE a.id = b.student_id and b.id_submission_type =1 and a.status_n='v' and a.id_academic_programe = p.id GROUP BY b.student_id) ) from academic_programe as p
But give me error
1054 - Unknown column 'p.id' in 'where clause'
Any suggestion? sory for my english
Correlations cannot be in nested subqueries. Fortunately, this is easy to fix:
select p.id,
(select count(*)
from student st join
submission su
on st.id = su.student_id and
su.id_submission_type = 1 and
st.status_n = 'v' and
where st.id_academic_programe = p.id
)
from academic_programe p;
Try this out:
select c.academic_program_name,count(a.distinct student_name) as count
from
(select * from student where status = 'v') a
inner join
(select * from submission id_submission_type=1) b
on a.id =b.student_id
inner join
academic_program_name c
on a.id_academic_programe = c.id
group by c.academic_program_name;
Let me know in case of any queries.
Please try the following...
SELECT student.id,
student_name,
academic_program_name AS Programe,
COUNT( status_n ) AS status_n_count
FROM student
JOIN Submission ON student.id = Submission.student_id
RIGHT JOIN academic_program ON student.id_academic_programe = academic_program.id
WHERE id_submission_type = 1
AND status_n = 'v'
GROUP BY student.id,
student_name,
academic_program_name;
This statement begins by joining the student and Submission so as to get a table containing the student's id, student_name, status_n and id_submission_type fields. This is then RIGHT JOINed to form a table where each academic program is listed along with each student's details, and that programs with no students are still listed.
The resulting dataset is refined as per your criteria with the WHERE clause, GROUPed and SELECTed
If you have any questions or comments then please feel free to post a Comment accordingly.
I have the next tables: faculty, faculty_subjects and subjects. Every faculty has a list of subjects
How can I SELECT subjects that are not needed for this faculty ?
Note: if user adds some new subject, then this subject doesn't already have a record in faculty_subjects table, but it should be displayed in SELECT.
So to sum up - from the list of ALL subjects that exists I want to know the ones that doesn't needed for this faculty. How this can be achieved ?
I'm using MySQL database.
PS. if there is a more better title - please edit.
You can LEFT JOIN to the Faculty_Subject, with the particulary Faculty ID you are interested in used in the join predicate, then filter out the rows with a match using the WHERE predicate, so you are left with the subjects you want:
SELECT s.id, s.Name
FROM Subject AS s
LEFT JOIN Faculty_Subject AS fs
ON fs.Subject_idSubject = s.ID
AND fs.Faculty_idFaculty = 1
WHERE fs.id IS NULL;
Note, I would usually advise to use NOT EXISTS syntax:
SELECT s.id, s.Name
FROM Subject AS s
WHERE NOT EXISTS
( SELECT 1
FROM Faculty_Subject AS fs
WHERE fs.Subject_idSubject = s.ID
AND fs.Faculty_idFaculty = 1
);
Which I beleive is more logical to the reader, but in MySQL LEFT JOIN/IS NULL performs better than NOT EXISTS
SELECT name FROM Subject WHERE id NOT IN
(SELECT FS.Subject_idSubject
FROM Faculty_Subjects AS FS
INNER JOIN Faculty AS F ON F.id=FS.Faculty_idFaculty
WHERE F.name='Physics' AND FS.Subject_idSubject IS NOT NULL)