There was an error parsing the query - mysql

I need help.... I am not good at SQL I get this error when I try to apply a JOIN:
[ Token line number = 1,Token line offset = 66,Token in error = JOIN ]
This is My SQL:
var query = "SELECT Team.TeamName, Fixtures.HomeTeam" +
"FROM Team" +
"LEFT JOIN Fixtures" +
"ON Team.TeamId=Fixtures.HomeTeam" +
"ORDER BY Team.TeamName";
Team Table Has PK: TeamId
Fixture Table Has FK: HomeTeam
I am using WebMatrix 2. Razor WebPages

No spaces between line concatenations. Change every line to include space at the end.
var query = "SELECT Team.TeamName, Fixtures.HomeTeam " +
"FROM Team " +
"LEFT JOIN Fixtures " +
"ON Team.TeamId=Fixtures.HomeTeam " +
"ORDER BY Team.TeamName";

As pointed by Charles Brentana, you have missed the spaces in your SQL command.
Maybe a better solution is to you use a verbatim string literal, i.e. a string created with an # character before the double-quote character, that can span multiple lines:
var query = #"SELECT Team.TeamName, Fixtures.HomeTeam
FROM Team
LEFT JOIN Fixtures
ON Team.TeamId=Fixtures.HomeTeam
ORDER BY Team.TeamName";

You need spaces between your strings.
I avoid this by putting the space as the first character, so it's really obvious when you forget to code it:
var query = "SELECT Team.TeamName, Fixtures.HomeTeam" +
" FROM Team" +
" LEFT JOIN Fixtures" +
" ON Team.TeamId=Fixtures.HomeTeam" +
" ORDER BY Team.TeamName";
If you consistently code this way you'll be able to spot any missing spaces instantly.

Related

Mysql Query With Like Operator and order by keyword giving empty records in spring boot api

native Query I Wrote in My Repository
If i remove order by condition in the below query , it is giving some records but i want it in some order so i added order by condition then it not showing records only . but the same mysql query with order by condition is giving some records in mysql workbench.
#Query(value = "SELECT l.id AS id,l.first_name AS firstName,l.last_name AS lastName,"
+ " l.email AS email,l.phone AS phone,l.place AS place,l.course_id AS courseId,"
+ " c.name AS courseName,l.source AS source,l.enquiry_for AS enquiryFor,"
+ " l.appointment_date AS appointmentDate,l.description AS description,"
+ " l.discount AS discount,l.status AS status,l.assignee_id AS assigneeId,"
+ " ase.first_name AS assigneeFirstName,ase.last_name AS assigneeLastName,"
+ " l.assignor_id AS assignorId,asr.first_name AS assignorFirstName,"
+ " asr.last_name AS assignorLastName,l.active AS active,l.created_date AS createdDate,"
+ " l.updated_date AS updatedDate,(SELECT comments FROM reviews where created_date IN"
+ " (SELECT MAX(created_date) from reviews where lead_id=l.id)) AS latestComment FROM "
+ " leads l JOIN users AS ase ON l.assignee_id = ase.id JOIN users AS asr ON "
+ " l.assignor_id = asr.id JOIN courses AS c ON l.course_id =c.id WHERE l.status!='Draft'"
+ " AND (l.first_name LIKE '%:keyword%' OR l.last_name LIKE '%:keyword%' OR l.phone LIKE"
+ " '%:keyword%') ORDER BY -l.appointment_date DESC,l.created_date ASC", nativeQuery=true)
List<Leads> searchLeadsForAdmin(#Param("keyword") String searchKeyword);
i don't know Where am i going wrong , attached image below mysql query result came in workbench.
I found the answer to my question. It was a concatenation issue. In the second last line of my query after 'l.phone LIKE' I added the value in the next line.
+ " AND l.first_name LIKE %:keyword% OR l.last_name LIKE %:keyword% OR "
+ " l.phone LIKE %:keyword% ORDER BY -l.appointment_date DESC,"
+ " l.created_date ASC", nativeQuery = true)
The above query works.

[21000][1242] Subquery returns more than 1 row

My Query:
entityManager.createQuery("SELECT " +
"q.id, " +
"q.title, " +
"q.user.fullName, " +
"q.user.reputationCount, " +
"q.viewCount, " +
"q.countValuable, " +
"q.persistDateTime, " +
"t.id, " +
"t.name, " +
"t.description, " +
"(SELECT COUNT (a) FROM Answer a WHERE a.question.id = q.id), " +
"(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id) " +
"FROM Question q JOIN q.tags t")
Here I get the error - [21000][1242] Subquery returns more than 1 row
By the method of exceptions, I determined that the error in this query string:
"(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id) "
How to make the correct request so that there is no this error? Thank!
Two common ways are aggregation and limiting:
(SELECT MAX(a.isHelpful) FROM Answer a WHERE a.question.id = q.id)
(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id LIMIT 1)
However, those are really just hacks to get around an "issue" with the data. I put issue in quotes, but the real issue is probably your understanding of data and not the data itself.
You should understand why there are duplicates. Then decide which value you want. And implement the correct logic for what you want.
Subquery returns more than 1 row, this simply means that your query is not returning a single row for the outer select statement to work.
"(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id) "
you have to apply a set of conditions to filter out your data uniquely or use joins to combine your table Answer and Question and then filter data accordingly.
you can also group each row data in one column by GROUP_CONCAT Mysql function like this :
"(SELECT GROUP_CONCAT(a.isHelpful) FROM Answer a WHERE a.question.id = q.id) "
Although GROUP_CONCAT is not available in Mysql, for that you can also bind SQL function in hibernate as described in this post.
After a day of various trial and error, I found the following solution, I hope someone will broaden their horizons and help in solving their problem:
entityManager.createQuery("SELECT " +
"q.id, " +
"q.title, " +
"q.user.fullName, " +
"q.user.reputationCount, " +
"q.viewCount, " +
"q.countValuable, " +
"q.persistDateTime, " +
"t.id, " +
"t.name, " +
"t.description, " +
"(SELECT COUNT (a) FROM Answer a WHERE a.question.id = q.id), " +
"(SELECT CASE WHEN MAX (a.isHelpful) > 0 THEN true ELSE false END FROM Answer a WHERE a.question.id = q.id) " +
"FROM Question q JOIN q.tags t")

Use CONCAT inside CONCAT in mysql

I have a stored procedure and a part of it is:
SET #TableAlias = " ls_flock AS _flk ";
SET #Select = CONCAT(
" _flk.Id as Id, _flk.Code as Code, ",
CONCAT(" _em.FirstName", " ", "_em.LastName "), -- here I want to CONCAT 2 columns as one field.
" as FlockManager"
);
SET #Join = " JOIN employee as _em ON _flk.EmployeeId = _em.Id ";
I am unable to CONCAT two columns as a field with a CONCAT function.
And if I remove following line, code works fine:
CONCAT(" _em.FirstName", " ", "_em.LastName "), -- here I want to CONCAT 2 columns as one field.
" as FlockManager"
I'm 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 '.LastName as FlockManager FROM ls_flock AS _flk JOIN employee as _em ON _flk.' at line 1
I suspect that you want to generate a query string that uses CONCAT() - because I see that the table alias _em is declared further in the #Join variable. For this, you would need to move the function inside the query string.
SET #Select = CONCAT(
" _flk.Id as Id, ",
"_flk.Code as Code, ",
"CONCAT(_em.FirstName, "" "", _em.LastName) s FlockManager"
;
And then, you just don't need the outer CONCAT():
SET #Select =
" _flk.Id as Id, _flk.Code as Code, CONCAT(_em.FirstName, "" "", _em.LastName) as FlockManager";

Select query with join -syntax error

SELECT SampleSheet.Id,SampleSheet.Sample_Complete,SampleSheet.SampleName,"
+ "count(Job.sampleId) AS NumberOfSamples FROM (SampleSheet "
+ "LEFT JOIN Job ON SampleSheet.Id = Job.sampleId) "
+ "WHERE SampleSheet.Sample_Complete=?"
+ "GROUP BY SampleSheet.Id
Can anynone please tell me whats wrong in this query. I am getting syntax error.
It works Fine if I dont use the Where Condition. But I need to get the rows for the ones where the sample is complete.
SELECT SampleSheet.Id,SampleSheet.Sample_Complete,SampleSheet.SampleName,count(Job.sampleId) AS NumberOfSamples FROM (SampleSheet LEFT JOIN Job ON SampleSheet.Id = Job.sampleId) GROUP BY SampleSheet.Id
There are couple of suggestions. It seems to be space issue after WHERE clause
+ "WHERE SampleSheet.Sample_Complete=?"
+ "GROUP BY SampleSheet.Id
instead
+ "WHERE SampleSheet.Sample_Complete=? "
+ "GROUP BY SampleSheet.Id
You could also remove ( just after FROM and obviously ) as well. It is not required.
FROM (SampleSheet "
+ "LEFT JOIN Job ON SampleSheet.Id = Job.sampleId) "

Delete query error for multiple records

I have a problem in this query:
string sqlString = "DELETE FROM [upload_news] WHERE (SELECT TOP " + no_of_recordss + " * FROM [upload_news] WHERE [country]='" + countryy.Text + "')";
Error Message :
Error: {"An expression of non-boolean type specified in a context
where a condition is expected, near ')'."}
How can i fix this ?
In the where clause you need a boolean expression.
Moreover, mysql doesn't support select top, you have to use limit instead and you can use it directly on delete
So your query should be:
delete from upload_news
where country=<SOME_COUNTRY> limit <NO_OF_RECORDS>
You have to replace values within "<>" with your desired values.
Or in your "strange" syntax:
string sqlString = "DELETE FROM [upload_news] WHERE [country]='" + countryy.Text + "' limit "+no_of_recordss;