开发者

MySQL Query 2 inner join?

I have a problem with my sql query in mysql. in sqlite3 and sql server all works.

SELECT        `buildings`.*
FROM          `buildings`
  INNER JOIN  "floors"
  ON          "floors"."building_id" = 开发者_Go百科"buildings"."id"
  INNER JOIN  "spaces"
  ON          "spaces".floor_id = "floors".id

maybe i need to process on other way in mysql?

thanks


MySQL treat words in quotes ("floors") as strings, so those values are NOT used as table/field names. Try

SELECT ...
...
INNER JOIN floors ON floors.building_id = buildings.id
INNER JOIN spaces ON spaces.floor_id = floors.id

instead. Backticks around table/field names are required ONLY when the table/field name is a reserved word. buildings is not a reserved word, so no backticks are necessary.


ANSI_QUOTES may not be enabled. From the manual ref:

If the ANSI_QUOTES SQL mode is enabled, it is also permissible to quote identifiers within double quotation marks:

mysql> CREATE TABLE "test" (col INT);
ERROR 1064: You have an error in your SQL syntax...
mysql> SET sql_mode='ANSI_QUOTES';
mysql> CREATE TABLE "test" (col INT);
Query OK, 0 rows affected (0.00 sec)

Make sure ANSI_QUOTES is enabled, or just stick with the traditional backtick (`)

This is also, from your vague question, assuming I have the correct problem to chase.


SELECT        `buildings`.*
FROM          `buildings`
  INNER JOIN  `floors`
  ON          `floors`.`building_id` = `buildings`.`id`
  INNER JOIN  `spaces`
  ON          `spaces`.`floor_id` = `floors`.`id`

or

SELECT        buildings.*
FROM          buildings
  INNER JOIN  floors
  ON          floors.building_id = buildings.id
  INNER JOIN  spaces
  ON          spaces.floor_id = floors.id

Do not use : ". This is for String in MySql.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜