Knexjs 2 Join a table from another Schema - mysql

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.

Related

'Cant get attributes from Select' while setting up Query for export data task in DBeaver

Hey so I'm trying to set up an export data task in DBeaver, because I want to export some data to another database and I want to query the date in beforehand. So I'm trying to use this SELECT statement
SELECT *
FROM resource
WHERE assignedteamid = 2 AND active;
which works fine on its own but when I'm trying to add this query in the Task setup screen and click on continue I get this Error
Can't get attributes from `SELECT * FROM resource WHERE assignedteamid = 2 AND active;`
Reason: SQL 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 'LIMIT 0, 1' at line 4
I already tried to change up the query but I have no clue what I need to do to fix this problem. I appreciate any help.
Thank you!

MySQL error "An alias was previously found" when an alias isn't in the query

I have a very odd error while trying to perform an update on a database. This is on an Ubuntu 16.04 server using MySQL 5.7.19-0ubuntu0.16.04.1. The query is:
UPDATE athlet_teamseason SET offkeyreturners = 'test' WHERE athlet_teamseason.id = 29701;
The MySQL error 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 'offkeyreturners = 'test' WHERE athlet_teamseason.id = 29701' at line 1
I am doing this in phpMyAdmin, and it gives a bit more information:
2 errors were found during analysis.
An alias was previously found. (near " " at position 50)
An alias was previously found. (near "'test'" at position 51)
If I try this update directly in the phpMyAdmin user interface (search for record, edit field value, submit form) it works, and the query shown is:
UPDATE athlet_teamseason SET offkeyreturners = 'test' WHERE athlet_teamseason.id = 29701;
which appears to be identical. HOWEVER, if I do a string comparison between the two I get:
So while they appear to be the same, there is a difference somewhere.
The queries were created from a table in a database, using concatenation and referencing cells in a source table. For example:
="UPDATE athlet_teamseason SET offkeyreturners = '"&data!I2&"' WHERE athlet_teamseason.id = "&data!A2&";"
I have thousands of these and they all produce the same error. I've done this dozens of times in older servers, might be an issue with MySQL 5.7?
Thanks to Uueerdo, I eliminated non-printing characters in my query.

Error in your SQL syntax for INNER JOINS

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.

Issues with the mysql query

i am trying to get the result from thquery, but i am getting error on the line 2, where i am using the cast function:
SELECT ticketstickets.`ID`, ticketsusers.`fname`, ticketsusers.`lname`, ticketstickets.`subject`,
ticketstickets.`created`, CAST(ticketstickets.modified AS VARCHAR(100)) as modified,
ticketstickets.`priority`, ticketstickets.`status`,
ticketsdepartments.`name` FROM ticketsusers, ticketstickets
LEFT JOIN ticketsdepartments ON ticketstickets.`DEPARTMENT_ID`= ticketsdepartments.`ID`
WHERE ticketstickets.parent = 0
AND ticketstickets.by=ticketsusers.ID
I tried with convert function too, but same error: [Err] 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
'VARCHAR(100)) as modified,
ticketstickets.priority, ticketstickets.`s' at line 2
Please see in the manual that a value cannot be casted to varchar(N). Instead you should cast to char(N) (so without "var").
use that instead
CAST(ticketstickets.modified AS CHAR(100))
Probably the issue is in your CAST statement.
Refer this post to correct your query:
How to use the CAST function correctly in a MySql SELECT statement?

Syntax for input parameters in a MySQL query

We recently switched a database from MSSQL to MySQL and the queries that uses parameters does'nt work anymore.
Here's an example of a query in MSSQL:
SELECT * FROM users u WHERE u.ID = :id
Normally, the parameter browser would popup and ask me for a value for :id, but in MySQL I get 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 ':id'
I tried using a # or ? instead of : and it does'nt work.
Thanks in advance for the help.
syntax is not the same
set #id:=123;
SELECT * FROM users u WHERE u.ID = #id;
Docs for User defined variables