having clause issue - mysql

Using the Employee and Department tables from previous question
I'm trying to build another combined table showing the number of employees that work in the headquarters and research departments.
I've tried this so far but keep getting errors on my having clause. Any suggestions?
mysql> select e.fname, d.dname
-> from department d
-> inner join employee e on e.dno = d.dnumber
-> group by e.fname
-> having d.dname='Headquarters','Research';
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 ''Research'' at line 5
mysql> select e.fname, d.dname
-> from department d
-> inner join employee e on e.dno = d.dnumber
-> group by e.fname
-> having d.dname=1,5;
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 '5' at line 5

use IN in WHERE clause.
SELECT...
FROM...
WHERE d.dname IN ('Headquarters','Research')
GROUP BY...

Related

Why does this MySQL error near group by statement?

I want to group my table with mem_num and it appears error saying that :
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 'group by m.mem_num' at line 6.
Here's my code.
select m.mem_num, count(d.vid_num) as total
from membership m
inner join rental r on m.mem_num = r.mem_num
inner join detailrental d on r.rent_num = d.rent_num
having count(d.vid_num) > 1
group by m.mem_num;
For a clearer view click here to view the image
:
output query
HAVING belongs after the GROUP BY
select m.mem_num, count(d.vid_num) as total
from membership m
inner join rental r on m.mem_num = r.mem_num
inner join detailrental d on r.rent_num = d.rent_num
group by m.mem_num
having count(d.vid_num) > 1;

SQL syntax error for deleting multiple rows from a subquery

I have a relational MySQL database using InnoDB which connects courses to course attendees. The problem with this database is that the course_id column in course_attendees was not set as foreign key. There are alot of courses missing where the course_attendees table is trying to refer to.
I wanted to delete those records, since the courses don't belong anymore. I wrote this select query which selects all the courses that should be deleted:
SELECT
ca.`id`
FROM `course_attendees` AS ca
LEFT JOIN `courses` c
ON ca.`course_id` = c.`id`
WHERE c.`id` IS NULL
Now, when I try to wrap this in a DELETE query with the use of a subquery like this:
DELETE FROM courses AS C1
WHERE C1.`id` IN (
SELECT
ca.`id`
FROM `course_attendees` AS ca
LEFT JOIN `courses` c
ON ca.`course_id` = c.`id`
WHERE c.`id` IS NULL
);
I get the following error:
[2018-08-30 08:34:26] [42000][1064] 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 'AS C1
[2018-08-30 08:34:26] [42000][1064] 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 'AS C1
[2018-08-30 08:34:26] WHERE C1.`id` IN (
[2018-08-30 08:34:26] SELECT
[2018-08-30 08:34:26] ca.`id`
[2018-08-30 08:34:26] FROM `course_attendees` AS c' at line 1
Since the SELECT query works, what is the problem here and how can I fix it?
EDIT
After Tim's answer, it got me into this error:
[HY000][1093] You can't specify target table 'courses' for update in FROM clause
Your outer delete query is not correlated to the subquery at all, so you don't logically need aliases:
DELETE
FROM courses
WHERE id IN (
SELECT id FROM
(
SELECT ca.id
FROM course_attendees AS ca
LEFT JOIN courses c
ON ca.course_id = c.id
WHERE c.id IS NULL
) t
);
I'm not sure that aliases are allowed in a delete statement on just a single table. They are allowed for a delete join, but you're not doing that.

mysql left join syntax error 1064

Really stumped on this one. It should be a simple left join to a second table but i'm getting a syntax error. Trying to get the last due date on incomplete items.
Code:
SELECT *
FROM TBLTICKETHEADER h,
LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETITEM
WHERE YEAR(COMPLETEDDATE) = 9999
GROUP BY HEADERID) ld ON ld.HEADERID = h.HEADERID
ERROR:
1064 - 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 'LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETIT' at line 3
You have a comma after your h on the from clause. Remove it and your query should run.
SELECT *
FROM TBLTICKETHEADER h
LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETITEM
WHERE YEAR(COMPLETEDDATE) = 9999
GROUP BY HEADERID) AS ld ON ld.HEADERID = h.HEADERID

MySQL 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

I have a SQL query like this:
SELECT E.snum
FROM Enrolled E
GROUP BY E.snum
HAVING COUNT (*) >= ALL (SELECT COUNT(*)
FROM enrolled E2
GROUP BY E2.snum)
But it generates an 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 ') >= ALL (SELECT COUNT() FROM enrolled E2 GROUP BY E2.snum)
LIMIT 0, 30' at line 1
What does the error mean? Why it doesn't allow me to use ALL operator?
Try this
SELECT E.snum, COUNT(E.snum)
FROM Enrolled E
GROUP BY E.snum
HAVING COUNT (E.snum) >= ALL (SELECT COUNT(*)
FROM enrolled E2
GROUP BY E2.snum)

SQL : Update table with select

I have a query like this
UPDATE t_prd_cost_compare
SET
2015_AUG_PRD_UNIT_PRICE=i.PRD_UNIT_PRICE,
2015_AUG_PRD_SELLING_PRICE=i.PRD_SELLING_PRICE,
2015_AUG_PRD_IN_PATIENT_LIST_PRICE=i.PRD_IN_PATIENT_LIST_PRICE,
2015_AUG_PRD_OUT_PATIENT_LIST_PRICE=i.PRD_OUT_PATIENT_LIST_PRICE
FROM (
SELECT PRODUCTID,PRD_UNIT_PRICE,PRD_SELLING_PRICE,PRD_IN_PATIENT_LIST_PRICE,PRD_OUT_PATIENT_LIST_PRICE
FROM t_product_catalog
LEFT JOIN T_adjust ON IAJ_PRODUCTID=PRODUCTID AND IAJ_ADJNO IS NULL
WHERE PRODUCTID>1 AND (DATE(IAJ_DATE) = '2015-01-01')
GROUP BY IAJ_PRODUCTID
) AS i
WHERE i.PRODUCTID = t_prd_cost_compare.PRODUCTID
I get error like this
Error Code: 1064
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 'FROM (
SELECT PRODUCTID,PRD_UNIT_PRICE,PRD_SELLING_PRICE,PRD_IN_PATIENT_LIST_PRI' at line 7
I done checked the select statement is correct, but I still get error!
Any idea?
Issue solved, here is the solution
Update
Competition as C
inner join (
select CompetitionId, count(*) as NumberOfTeams
from PicksPoints as p
where UserCompetitionID is not NULL
group by CompetitionID
) as A on C.CompetitionID = A.CompetitionID
set C.NumberOfTeams = A.NumberOfTeams
refer from: mysql update query with sub query