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';
Related
I am trying to have an if else condition in this select statement. I am either passing a customerID or a accountID into this mapper and thus I need to check which one is being passed. I had the first case where there was no customerId (thus accountId is passed), and so the inner join is performed. The second one is when there is no accountID (thus customerId was passed). Then at the end of the cases, I want to order it by descending dates.
SELECT i.invoice_id, i.account_id, total_amount_due, due_date, bp.eff_date, bp.end_date, total_amount_due
(
CASE
WHEN customerId = null
THEN INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id
WHERE account_id=#{accountId}
ELSE INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id
INNER JOIN server.cust_acct_rel car ON car.account_id = i.account_id
WHERE car.customer_id=#{customerId}
END)
ORDER BY end_date DESC
However, when I run this, I get this error.
org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: java.sql.SQLSyntaxErrorException: 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 'INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id WHERE ' at line 5
Does anyone know what I am doing wrong here?
use IFNULL
select ...,..., IFNULL(customerID,accountID) newid
from
table1
left outer join table2
order by date desc
I'm trying to update my riders total ucipoints by following an example, with some small modifications, but I just get an error from it.
example
UPDATE P
SET extrasPrice = t.TotalPrice
FROM BookingPitches AS P INNER JOIN
(
SELECT
PitchID,
SUM(Price) TotalPrice
FROM
BookingPitchExtras
GROUP BY PitchID
) t
ON t.PitchID = p.ID
I got the code from this answer:
SQL Update to the SUM of its joined values
My code looks like this:
UPDATE P
SET ucipoeng = t.TotalPoints
FROM rytterlagsesong AS P INNER JOIN
(
SELECT
rytterid,
SUM(poeng) AS TotalPoints
FROM
t_ucipoeng
WHERE year(dato)='2016'
GROUP BY rytterid
) t
ON t.rytterid = P.rytterid AND t.sesong='2016'
I get the error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM rytterlagsesong AS P INNER JOIN ( SELECT rytterid, SUM(ucip' at line 3
Can someone help me find the error?
DB Structure:
rytterlagsesong: rytterid - ucipoeng - sesong
t_ucipoeng: rytterid - dato - poeng
So I want to sum the points (poeng) of all races in 2016 (dato=date) for a rider
And update that riders totalpoint (ucipoeng) for this season (sesong)
The update / join syntax is different per database. For mysql, use this:
UPDATE rytterlagsesong r
INNER JOIN (
SELECT rytterid, SUM(poeng) AS TotalPoints
FROM t_ucipoeng
WHERE year(dato)='2016'
GROUP BY rytterid
) t ON t.rytterid = r.rytterid AND t.sesong='2016'
SET r.ucipoeng = t.TotalPoints
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 ?
I have a problem with the following statement:
SELECT SUM(foreseen_charges.commonCharge) as required, foreseen_charges.flatsId
LEFT JOIN (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps
ON foreseen_charges.flatsId = deps.flatsId
FROM foreseen_charges GROUP BY foreseen_charges.flatsId
And I always getting this 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 'LEFT
JOIN (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps
ON f' at line 2
Could anyone help me?
Best Regards,
Cs
Put FROM before LEFT JOIN
SELECT SUM(foreseen_charges.commonCharge) as required,
foreseen_charges.flatsId
FROM foreseen_charges
LEFT JOIN (
SELECT deposits.flatsId
FROM deposits
GROUP BY flatsId
) deps ON foreseen_charges.flatsId = deps.flatsId
GROUP BY foreseen_charges.flatsId
You're doing things out of order. The general structure is:
SELECT (something) FROM table JOIN other-table ON table.var1=other-table.var2 WHERE situation
You're doing this:
SELECT (something) LEFT JOIN table ON table.var1=other-table.var2 FROM other-table
You can't join until you've declared both tables, and you can't relate other-table because you haven't declared it yet.
Unsure what the query is meant to accomplish, but this should fix your syntax error:
SELECT SUM(foreseen_charges.commonCharge) as required, foreseen_charges.flatsId
FROM foreseen_charges
LEFT JOIN (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps
ON foreseen_charges.flatsId = deps.flatsId
GROUP BY foreseen_charges.flatsId
I have following query that is working fine in TSQL
SELECT
shoppingcart_1.price, shoppingcart_1.stid, course.isbn,
book.BookTitle, course.Course_ID, schedule.stid AS Expr1
FROM
book
INNER JOIN
shoppingcart AS shoppingcart_1
INNER JOIN
schedule ON shoppingcart_1.cid = schedule.course_ID
INNER JOIN
course ON schedule.course_ID = course.Course_ID
ON book.isbn = course.isbn
WHERE
(shoppingcart_1.stid = '20070004')
but when I run it in Mysql it displays error on line
INNER JOIN course ON schedule.course_ID = course.Course_ID ON book.isbn = course.isbn
Error text is
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 'ON book.isbn = course.isbn WHERE (shoppingcart_1.stid =
'20070004') LIMIT 0' at line 6
I am writing queries first time in mysql, please help
Seems like you have your joins a little mixed up. I tried cleaning it up. See if this works for you
SELECT shoppingcart_1.price, shoppingcart_1.stid, course.isbn, book.BookTitle, course.Course_ID, schedule.stid AS Expr1
FROM book
INNER JOIN course ON book.isbn = course.isbn
INNER JOIN schedule ON course.Course_ID = schedule.course_ID
INNER JOIN shoppingcart AS shoppingcart_1 ON schedule.course_ID = shoppingcart_1.cid
WHERE shoppingcart_1.stid = '20070004'
Note how I join book to course, then course to schedule, then schedule to shoppingcart. Then use the WHERE clause to specify other conditions.