I'm having an issue with the following query. It works in mysql workbench but not when I use it in java with jdbc. I keep getting a syntax error.
Here is the query:
"SELECT f.ISBN, f.text, m.title, AVG(r.rating) as score" +
" FROM RATES r LEFT OUTER JOIN FEEDBACK f ON (f.fid = r.fid) WHERE f.ISBN = ? " +
"LEFT OUTER JOIN MOVIE m ON (m.ISBN = f.ISBN) " +
"GROUP BY ISBN " +
"ORDER BY score DESC LIMIT ? ";
I did some searching and found a jdbc escape syntax of {oj }. But I would get another syntax error.
The error I am getting lately is:
HTTP Status 500 - javax.servlet.ServletException:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 OUTER JOIN
MOVIE m ON (m.ISBN = f.ISBN) GROUP BY ISBN ORDER BY score DESC L' at
line 1
I would love some fresh eyes on this since I can't seem to see my issue.
Thanks in advance!
I think that WHERE clause has to be placed behind JOINING. And next problem is that number of columns in select clause has equal to number of columns in group clause(except functions as avg, count etc.). So you need to correct your query like this:
SELECT f.ISBN, f.text, m.title, AVG(r.rating) as score
FROM RATES r
LEFT OUTER JOIN FEEDBACK f ON (f.fid = r.fid)
LEFT OUTER JOIN MOVIE m ON (m.ISBN = f.ISBN)
WHERE f.ISBN = ?
GROUP BY f.ISBN, f.text, m.title
ORDER BY score DESC LIMIT ?
The left join must be before the where clause. And every column listed in the select clause should be in the group by clause:
SELECT f.ISBN, f.text, m.title, AVG(r.rating) as score
FROM RATES r
LEFT OUTER JOIN FEEDBACK f ON (f.fid = r.fid)
LEFT OUTER JOIN MOVIE m ON (m.ISBN = f.ISBN)
WHERE f.ISBN = ?
GROUP BY f.ISBN f.text, m.title
ORDER BY score DESC
LIMIT ?
Related
I have a query that works perfectly, however I need to change it a bit but it shows me an error and I can't figure out why. Below is the code before and after the changes I made:
BEFORE:
SELECT *,
(SELECT GROUP_CONCAT(pho_file_name) FROM post_images WHERE pho_post_id=posts.ID) AS photo_file_array
FROM users
INNER JOIN posts ON users.Id = posts.post_author
ORDER BY posts.ID;
AFTER:
SELECT *,
(SELECT GROUP_CONCAT(pho_file_name) FROM post_images WHERE pho_post_id=posts.ID) AS photo_file_array
FROM users WHERE users.Id = "1"
INNER JOIN posts ON users.Id = posts.post_author ON posts.post_date = "2020-12-04 07:51:21"
ORDER BY posts.ID;
It shows me the following 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 'INNER JOIN posts ON users.Id = posts.post_author AND posts.post_date "2020-12...' at line 4
I'm a newbie on MySql but from what I can understand I think the error occurs because of the the double ON inside the INNER JOIN. So, is it possible to add multiple ON inside the INNER JOIN? Thanks in advance!!
You have a few syntax issues, you can't put joins and where anywhere, you also need to use the correct delimiters and data types.
Try the following and note using table and column aliases makes for an easier-to-read query.
Additionally, consider not using select * and reference only the columns you actually require, if possible.
SELECT u.*, p.*, (
SELECT GROUP_CONCAT(i.pho_file_name)
FROM post_images i
WHERE i.pho_post_id = p.ID
) AS photo_file_array
FROM users u
JOIN posts p ON p.post_author = u.Id
AND p.post_date = '2020-12-04 07:51:21'
WHERE u.Id = 1
ORDER BY p.ID;
Here is a full working query. The errors (double ONclause, WHERE clause in the wrong position, wrong quotes) are corrected. Moreover, the ID is compared to an integer now and the post_date to a timestamp literal. I've used table aliases to get this more readable.
SELECT
u.*,
p.*,
(
SELECT GROUP_CONCAT(pi.pho_file_name)
FROM post_images pi
WHERE pi.pho_post_id = p.id
) AS photo_file_array
FROM users u
INNER JOIN posts p ON p.post_author = u.id
AND p.post_date = TIMESTAMP '2020-12-04 07:51:21'
WHERE u.id = 1
ORDER BY p.id;
As to the tables: I suggest you are more consistent with your column names. Why do you call the post ID post_author? One would assume a name here. Just call it post_id in every table. And you don't have to precede columns with abreviations like pho. Just qualify all columns with their tables like I did in my query.
I am getting a syntax error with the code below and I can't pinpoint what's wrong.
SELECT contrat.nomPrenom
FROM contrat
WHERE Type_emploi LIKE 'Acteur'
INNER JOIN film
ON contrat.ID_film = film.ID_film
AND film.Note IN (
SELECT Note
FROM film
ORDER BY Note
DESC
LIMIT 1
)
WHERE Type_emploi LIKE 'Acteur' INNER JOIN film
Error is here: you have to use INNER JOIN before using WHERE clause.
Try inner join before the where clause
It's probably something I can't seem to understand with MySQL, but after wasting my day going through StackOverflow's related questions without fixing the issue, I decided to ask about it.
SELECT users.idUser, users.name, categoryName
FROM users
LEFT JOIN (
SELECT `translation` as categoryName
FROM localization,
usercategories
WHERE localization.`string` = usercategories.name
AND usercategories.idUserCategory = users.idUserCategory
)
as Something
WHERE users.idUser != 1
ORDER BY users.name ASC
Whichever query I tried today that would include a subquery, I would get the same syntax error at pretty much the same place: right after the subquery's alias (in this case, Something).
#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 'WHERE users.idUser != 1 ORDER BY users.name ASC LIMIT 0, 30' at line 11
This issue here is that you're missing the ON clause of your join. You need to select a condition to join the two tables together, like this:
SELECT stuff
FROM stuff
LEFT JOIN (other stuff)
ON stuff.something = otherstuff.something. // Add here.
You're JOIN criteria is non-ANSI and does not have an ON clause ... perhaps that is causing it? Try this, a bit more optimized:
SELECT Usr.idUser AS idUser
,Usr.name AS name
,UsrCat.translation AS categoryName
FROM users AS Usr
LEFT OUTER JOIN usercategories AS UsrCat
ON UsrCat.idUserCategory = Usr.idUserCategory
LEFT OUTER JOIN localization AS Lcl
ON Lcl.string = UsrCat.name
WHERE Usr.idUser <> 1
ORDER BY Usr.name ASC
No need for subquery, should be pretty performant.
You could re-organize your query so that it does not need a sub-query. This would also allow you the benefit of adding more columns to the select from any of the tables. Also, it is more correct.
SELECT
users.idUser,
users.name,
localization.`translation` as categoryName
FROM users
LEFT JOIN usercategories ON usercategories.idUserCategory = users.idUserCategory
LEFT JOIN localization ON localization.`string`= usercategories.name
WHERE users.idUser <> 1
ORDER BY users.name ASC
I try to select the the rows with the newest timestamp in change_date from a table in a LEFT JOIN. I really don't know why this query fails:
SELECT
i.ID, i.title, i.create_date,
u1.username creator_name,
u2.username assignee
FROM item i
LEFT JOIN user u1 ON u1.login_IDFK = i.creator_IDFK
LEFT JOIN user u2 ON u2.login_IDFK = i.assigned_to_IDFK
LEFT JOIN (
SELECT MAX(change_date), item_IDFK FROM item_state GROUP BY item_IDFK
) AS ist ON ist.item_IDFK = i.ID
I get 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 ') AS ist ON ist.item_IDFK = i.ID' at line 2 (Code: 1064)
Query works great without the last LEFT JOIN
(SELECT change_date, item_IDFK FROM item_state GROUP BY item_IDFK)
You are using a group by clause without an aggregate. Each item in the select list must either be represented in the group by clause, or be part of an aggregate expression
I.E.
(Select Max(Change_Date), item_IDFK from item_state group by item_IDFK)
try to save your last subquery in a view table, and after that, left join from that table, and see if the syntax error persists.
I have this query and appearently it's faulty?
I'm trying to join fices with mems so I can have the ficeID along with all the results from mems(These queries work individually). What am I doing wrong?
SELECT *
FROM mems
WHERE deleted <> -1
ORDER BY sort_mems
LEFT JOIN SELECT ficeID
FROM fices
Result:
#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 ficeID FROM offices LIMIT 0, 30' at line 1
You have JOIN clause after ORDER BY. You should place it in FROM.
I suggest you define condition of a LEFT JOIN
Also, I suggest you surround you temp tables with brackets:
SELECT m.*, t1.officeID
FROM members m
LEFT JOIN offices t1
ON m.memberID = t1.memberID
WHERE m.deleted <> -1
ORDER BY m.sort_membername;
Yes, you have the LEFT JOIN in the wrong spot (it should go after your FROM clause, and you also seem to be missing a join criteria (the ON part, this tells the database how these tables are related):
SELECT *
FROM mems m
LEFT JOIN fices f
ON m.??? = f.???
WHERE deleted <> -1
ORDER BY sort_mems