Error in your SQL syntax for INNER JOINS - mysql

I have been trying to run this mysql query through rstudio, but I keep getting an error. Any idea why?
Here is my query:
SELECT host.key AS 'uid',
daily_summary.date AS 'date'
FROM host INNER JOIN daily_summary
USING(weekly_id);
This is the error I get.
42000 1064 [MySQL][ODBC 5.1 Driver][mysqld-5.5.40-
0ubuntu0.14.04.1]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 host INNER
JOIN daily_summary USING(weekly_id)' at line 1

This is the query as it would better be written:
SELECT host.key AS uid, daily_summary.date
FROM host INNER JOIN
daily_summary
USING(weekly_id);
In addition to removing the spurious commas, this also removes the unneeded quotes around the column aliases. Only use single quotes for string and date constants. Otherwise, they are likely to cause confusion in queries. If you need to escape aliases in MySQL, then use backticks.

Check version simplified without typos:
SELECT host.key, daily_summary.date
FROM host INNER JOIN daily_summary
USING(weekly_id);
I also prefer to use word ON instead of USING:
SELECT host.key, daily_summary.date
FROM host INNER JOIN daily_summary
ON host.weekly_id = daily_summary.weekly_id

The MySQL syntax error message shows you part of your statement starting with the first character it can't understand. In your case the first characters it can't understand are FROM. It's looking for another column name after the comma.

Related

A query on the recipesexample database from SQL for mere mortals

What's the problem with this query?
It's only a slight modification from the SQL for mere mortals book...
Select r.RecipeTitle,
from (Select RecipeClassID
from recipe_classes as RC
where RC.RecipeClassDescription like "Main%"
or RC.RecipeClassDescription="Dessert") as rcfiltered
inner join recipes as r
on rcfiltered.RecipeClassID = r.RecipeClassID;
Remove the comma:
Select recipes.RecipeTitle,
from ...
There must be no comma following the last expression in the select-list.
WRONG:
SELECT A, B, C, FROM ...
RIGHT:
SELECT A, B, C FROM ...
With the extra comma, the query produces this error in MySQL:
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 'from (Select RecipeClassID
from recipe_classes as RC
where RC.Recipe' at line 2
Here's a tip on how to read syntax error messages: It tells you exactly where the parser got confused: "near 'from ..." This probably means something you wrote in the query immediately prior to that position was wrong. Knowing this helps to narrow down the cause of the syntax error.
If the error says, "near ''" then it means it got to the end of the query and then got confused. Maybe you opened a parenthesis but forgot to close it for example.
That's the first problem that jumps out at me. I don't know the original query you were modifying, so I don't know how you changed it. I don't know if the query does what you intend it to do, but aside from the comma issue it looks like the syntax is valid.

Knexjs 2 Join a table from another Schema

i'm trying to join a table from the database account into the database player. Table names are player, player and account, account. I have account_id in table player and id in table account.
So i tried this code but i get following error with nodejs:
Unhandled rejection Error: ER_PARSE_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 '.account on player.account_id = account.id' at line 1
This is my code:
knex.withSchema('player').table('player').select('player.*').leftJoin(knex.withSchema('account').table('account'),'player.account_id','account.id')
I have to use Mysql 5.6. Appreciate every solution
I don't see any straightforward approach using withSchema as its one schema all over the builder block. I used joinRaw() in my use case.
knex.withSchema('player').table('player').select('player.*')
.joinRaw('left join ?? on ?? = ??',['account.account','player.account_id','account.id'])
output:
select `player`.* from `player`.`player`
left join `account`.`account` on `player`.`account_id` = `account`.`id`
Note: Positional bindings ? are interpreted as values and ?? are interpreted as identifiers.

STRING_SPLIT in MySql- how to do it?

SELECT er.pNumber, er.name, ep.fPosition, eo.res
FROM events_shot er, events_shot_final ep, events_shot_final_res eo, events_gear era
WHERE era.idShot=er.idShot AND ep.idPhoto=era.idPhoto AND eo.idShot=era.idShot
AND era.idShot=42 AND eo.shotType='PRT'
AND er.pNumber IN (
SELECT *
FROM STRING_SPLIT(eo.photosId,'-')
)
shotsId is a String like 12-1-8-7... with n pNumber id separated by '-'
Unfortunately the query return this error:
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 '(eo.photosId,'-') )' at line 7
I can't change the database, how can I change my query?
Any help you can provide would be appreciated.
Fix your data model! Don't store multiple values in a single column.
Learn proper SQL syntax! Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
With that said, you don't need to split the string. You can use string operations:
AND CONCAT('-', eo.photosId, '-') LIKE CONCAT('%-', er.pNumber, '-%')
But you should really start work on fixing the data model as soon as you get this query to work.

Syntacts error in mysql query

Someone gave me this query to delete community from a database on my server using phpMyAdmin, it worked when he use it so I asked him to send it to me, but I get a error
MySQL said:
Documentation
#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 '"SELECT * FROM `connections` WHERE 1" :
delete from connections where communit' at line 1
I did a search on the error but could not figure it out.
"SELECT * FROM `connections` WHERE 1" :
delete from connections where community="XYZ"
You should separate your queries with ; and not :
The error you are receiving as you've shown it is because this line is incorrect:
SELECT * FROM `connections` WHERE 1"
First because it's ending in an unnecessary double quotation mark. MySQL does not use double quotes but single quotes, and even still the other single quote is not there to match it.
The single quotes are also a problem for community="XYZ", this should read: community = 'XYZ'
Secondly, you don't have a condition for your where statement, you must be missing something like:
WHERE columnName = 1;
If you were trying to select everything from connections, you can just remove that where clause all together.
EDIT
In addition, MySQL queries are separated by a semi-colon, not a colon, so MySQL will not realize you are trying two different queries.

MYSQL: Check if table exists does not work

IF OBJECT_ID(N`db_291702_2`.`aaCoRrankingDateManage`, N'U') IS NOT NULL
BEGIN
PRINT 'Table Exists'
END
What is wrong with this? Why do I get errors?
Neither of the suggested ways in how-to-check-if-a-table-exists-in-sql-server/ works for me.
PS. "does not work" means errors like
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 ''db_291702_2'.'aaCoRrankingDateManage' LIMIT 0, 30' at line 1
Additional info:
I am using phpMyAdmin, my two databases are called db_291702_1 and db_291702_2, the latter has two tables, one of them is called aaCoRrankingDateManage
If you want to escape table or column names then use backticks
select `group` from table1
A static string must be included in quotes
select * from users where name = 'john'
And the syntax of every DB engine is a little bit different. The above works for MySQL, but SQL-Server has a different syntax. There you use brackets [] to escape names.
But you only need to escape names if you use reserved words. You don't have to escape everything.
The given source code is no MySQL Code.