Hi all I have the following query
SELECT MAX(s.nextPass), st.ID, st.fio
FROM recomission s
JOIN recomission_to_student b
ON s.ID = b.recomission_id
JOIN student st
ON b.student_id = st.id
JOIN education_to_student e
ON s.ID = e.education_id
JOIN education ed
ON e.student_id = ed.id
WHERE s.nextPass BETWEEN '2015-11-09' AND '2016-02-09'
GROUP BY st.fio
The tables are: recomission, student and education they are related using bridge tables lide recomission_to_student, education_to student. The query works just fine until I am trying to fetch departmant from education table but then I do it query returns null. I do understand that I somewhere close to desition but still can't figureout what to do.
as for now trying to bypass the problem using subquery instead of join
SELECT MAX(s.nextPass), st.ID, st.fio
FROM recomission s
JOIN recomission_to_student b
ON s.ID = b.recomission_id
JOIN student st
ON b.student_id = st.id
WHERE (select education_id where student_id=st.ID) and s.nextPass BETWEEN '2015-11-09' AND '2016-02-09'
and getting the following error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where student_id=st.ID)' at line 8
run the query only on recomission table , if you received results probably you have problems with your joins between the tables
SELECT MAX(s.nextPass) , st.fio
FROM recomission s
WHERE s.nextPass
BETWEEN '2015-11-09'
AND '2016-02-09'
GROUP BY st.fio
Your query has an error in regards to grouping. You select MAX(s.nextPass) and st.fio which is ok as MAX is a group function (aggregate function) and st.fio is mentioned in the group by.
BUT st.ID is neither a group function nor is it mentioned in the group by. Thus your query should return an error. The null could be caused because of this error (although normally I would expect an error message instead there).
SELECT MAX(s.nextPass) ,st.ID, st.fio FROM recomission s JOIN recomission_to_student b ON s.ID = b.recomission_id
JOIN student st ON b.student_id = st.id
JOIN education_to_student e ON s.ID = e.education_id
JOIN education ed ON e.student_id = ed.id
WHERE s.nextPass
BETWEEN '2015-11-09'
AND '2016-02-09'
GROUP BY st.fio
Thus either you change the group by to:
GROUP BY st.fio, st.id
or remove st.ID from the select.
Your table :
s, b, st, e, ed
There are this tables have s=b, b=st and s=e, e=ed this equations.
You must check this equations and are you sure for s.nextpass between '2015-11-09' and '2016-02-09' this condition, maybe there are no records ?
Related
Good afternoon, I’m trying to fulfill the request while writing an error. Error # 1066 does not quite understand how it can be fixed in my particular case. Perhaps the problem is that I connect to the table several times and need an alias.
SELECT `employees`.`name`, `employees`.`surname`, `employees`.`patronymic`,
`doc`.`name`, `doc`.`agreement`, `tank`.`name`,
`liquid`.`name`, `WorkPlan`.`description`
FROM `WorkPlan` , `employees` , `doc` , `tank` , `liquid`
LEFT JOIN `WorkPlan` ON `tank`.`id` = `WorkPlan`.`id_tank`
LEFT JOIN `WorkPlan` ON `liquid`.`id` = `WorkPlan`.`id_liquid`
LEFT JOIN `WorkPlan` ON `doc`.`id` = `WorkPlan`.`id_doc`
AND `WorkPlan`.`id_tank` = `tank`.`id`
AND `WorkPlan`.`id_liquid` = `liquid`.`id`
AND `WorkPlan`.`id_doc` = `doc`.`id`
I suspect (by your SELECT list of columns) that you want to join employees to the other tables.
For every join you must specify in the ON clause the columns that relate the 2 tables and it is a good practice to use aliases for the tables which shorten the code and make it more readable:
SELECT e.name, e.surname, e.patronymic,
d.name, d.agreement,
t.name,
l.name,
w.description
FROM employees e
LEFT JOIN WorkPlan w ON e.? = w.?
LEFT JOIN tank t ON t.id = w.id_tank
LEFT JOIN liquid l ON l.id = w.id_liquid
LEFT JOIN doc d ON d.id = w.id_doc
Replace the ? with the names of the columns that relate employees with WorkPlan.
I don't know why this my DELETE sql command got the error number #1064.
My delete sql:
delete from nit.grades g where
(select id_dep from nit.offers as oo,nit.subjects as s where s.id=oo.id_subject and g.id_offer=oo.id)
!=(select id_dep from nit.students as stu where g.id_student=stu.id);
but this sql Select same where clause is working.
select * from nit.grades g where
(select id_dep from nit.offers as oo,nit.subjects as s where s.id=oo.id_subject and g.id_offer=oo.id)
!=(select id_dep from nit.students as stu where g.id_student=stu.id);
thanks for any help.
the Error message:
The syntax with the alias of the table you use for the delete statement is wrong.
Even more in the subqueries you use the target table and this is not allowed in MySql.
Instead you should use joins.
From your code this is what I understood that you want:
delete g
from nit.grades g
inner join nit.offers oo on g.id_offer = oo.id
inner join nit.subjects s on s.id = oo.id_subject
inner join nit.students st on g.id_student = st.id
where st.id_dep <> s.id_dep
In the WHERE clause I'm not sure if the columns id_dep are qualified correctly because they are not qualified also in your code.
If this is not what you want to do then use your SELECT query which does work (as you say) as a join to the table, provided there is a primary key like id in nit.grades:
delete g
from nit.grades g
inner join (
<your select query here>
) t
on t.id = g.id
Here is the complex query that I wrote but can't figure out what's the problem:
SELECT student.progid,
student.batch
FROM (student
JOIN registers
ON student.studentid = registers.studentid)
JOIN (SELECT offers.courseno
FROM (offers
JOIN instructor
ON offers.instructorid = instructor.instructorid))
ON offers.courseno = registers.courseno
WHERE instructor.instructorname = 'P M Jaat'
AND ( offers.acadyear LIKE '2007%'
OR offers.acadyear LIKE '2008%'
OR offers.acadyear LIKE '2009%'
OR offers.acadyear LIKE '2010%'
OR offers.acadyear LIKE '2011%' );
This results in an error, but I'm going to leave it to others to tell you what that is:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'R1
JOIN (SELECT
Double check your parenthesis. Your FROM clause is
(student JOIN registers ON student.StudentID=registers.StudentID)
Which doesn't make sense.
What you want is:
FROM (student)
JOIN registers AS r1
ON (student.StudentID=registers.StudentID)
Or you can just lose the extra parenthesis:
FROM student
JOIN registers AS r1
ON student.StudentID=registers.StudentID
P.S. You also had your AS r1 in the wrong spot.
UPDATE: You need parenthesis when you are using subqueries.
FROM student
JOIN registers
ON student.studentid = registers.studentid
JOIN(
SELECT courseno
FROM offers
JOIN instructor
ON offers.instructorid = instructor.instructorid
) AS r2 ON offers.courseno = registers.courseno
Perhaps you're after something like this...
SELECT r.progid
, r.batch
FROM student s
JOIN registers r
ON r.studentid = s.studentid
JOIN offers o
ON o.courseno = r.courseno
JOIN instructor i
ON i.instructorid = o.instructorid
WHERE i.instructorname = 'P M Jaat'
AND o.acadyear BETWEEN '2007-01-01' AND '2011-12-31';
I have the following MySQL query:
select c.company_id,c.company_name, pe.pe_relationship, opt.oo_type
from pub_entity pe
inner join company c
on c.company_id = pe.pe_company_id
inner join opt_out opt
on opt.oo_company_id=c.company_id
where pe.pe_pn_id in
(SELECT pn_id
FROM pub_notice
WHERE pn_company_id=2523);
and opt.oo_type not in
('image','iframe')
However, when I run the query, in opt.oo_type column I am still getting image and iframe results.
opt.oo_type is of type enum('image','iframe','other') Can anyone tell me why I am still getting those results?
You have a ; at the end of your first WHERE clause, which is cutting off your NOT IN clause. Move it to the end of the query:
select c.company_id,c.company_name, pe.pe_relationship, opt.oo_type
from pub_entity pe
inner join company c on c.company_id = pe.pe_company_id
inner join opt_out opt on opt.oo_company_id=c.company_id
where pe.pe_pn_id in (SELECT pn_id FROM pub_notice WHERE pn_company_id=2523)
and opt.oo_type not in ('image','iframe');
I am writing an query in sql and getting an error:
Invalid use of group function
What does it mean?
In my query, where clause is given below:
select c.name,s.contact,s.number
from list c , senior s
where c.id = s.id AND c.name = 'Abg' AND c.state ='qw' AND MIN(c.dob);
Basically, I have 2 files and I need to find the younger customer from 2nd file and then have to retrieve its data from first file. I have the ID number of customers in 2nd file. I first check the ids with the id of first file. And check its state and name. And then I need to find younger among those customers.Thats Why i need MIn function.
You need to use a subquery:
select c.name,s.contact,s.number
from from list c, senior s
inner join
(
select MIN(c.dob) minDob
,c.id
from list c
where c.id = s.id AND c.name = 'Abg' AND c.state ='qw'
group by c.id
) sq
on c.dob = sq.minDob
and c.id = sq.id
AND MIN(c.dob); is causing the error.
I think you should use something like:
c.dob = (select MIN(dob) from c);