2
votes

I would like to get rows from another database so I created query:

SELECT * FROM database-test.users

MySQL result that error:

Database_Exception [ 42000 ]: SQLSTATE[42000]: Syntax error or access violation: 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 '-test.users' at line 1

How to solve this?

Thanks for reply

3

3 Answers

3
votes

You need to do it like below (use back-ticks around table name):-

SELECT * FROM `database-test`.users

Or

SELECT * FROM `database-test`.`users`
0
votes

I would recommend to 'back tick' all database and table names in your query. It will tell the database's SQL parser to ignore any special characters such as "-" and consider them as part of the name.

Example:

SELECT * FROM `database-test`.`users`
0
votes

Try

SELECT * FROM `database-test`.users

As you can see, I have used the ` character to encapsulate database-test, which makes sure that non alpha-numeric characters, like - will be accepted in the name.