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;
Related
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
I have this query in which I am using Group By with ROLLUP and it works perfectly in MySQL.
SELECT r.Region_Name
, ad.District_Name
, COUNT(me.employee_id)
FROM `master_employee` me
INNER JOIN app_district ad
ON me.`district_id` = ad.`DISTRICT_UID`
INNER JOIN region r
ON ad.REGION_UID = r.REGION_UID
GROUP BY ad.REGION_UID, ad.District_Name WITH ROLLUP
HQL
SELECT new com.eld.chart.ChartValues (r.regionName, dm.districtName, STR(COUNT(me.districtModel)))
FROM MasterEmployeeModel me JOIN me.districtModel dm JOIN dm.regionModel r
GROUP BY ROLLUP(me.districtModel, r.regionName)
shows following exception
17:13:51,311 ERROR [org.hibernate.util.JDBCExceptionReporter] (http-localhost-127.0.0.1-8080-2)
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 '(masterempl0_.district_id, divisionmo2_.REGION_NAME)' at line 1
Is there any valid alternative to ROLLUP for hibernate?
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
I have two DBs- RATINGSAPP and MIGRATIONDATA.
I want to update a table in RATINGSAPP with some values in a table in MIGRATIONDATA. I am trying to run this query:
update r set internal_id = m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
This gives me 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
'from ratingsapp.hotel03 as r inner join migrationdata.migration as m on r.hotel_' at line 1
But a similar select query works for me and gives proper results.
select r.hotel_id, m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
What I am doing wrong in the update query?
The correct MySQL syntax is:
update ratingsapp.hotel03 r inner join
migrationdata.migration as m
on r.hotel_id = m.restaurant_id
set internal_id = m.internal_id ;
There is no from clause in a MySQL update. You are using SQL Server/Postgres syntax.
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...