MySql错误,尝试使用INNER JOIN从两个表中选择数据

时间:2014-11-05 00:50:56

标签: mysql sql database

当我尝试从数据库中的两个表中选择数据时,我收到此错误。

  

#1064 - 您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在' INNER JOIN附近使用hbr_galleri       ON hbr_billede.GalleriID = hbr_galleri.GalleriID       限制'在第4行

这是查询:

(抱歉丹麦表/列名称)

SELECT 
    `hbr_billede.BilledeID`, 
    `hbr_billede.GalleriID`, 
    `hbr_billede.FilePath`, 
    `hbr_billede.FilePathThumb`, 
    `hbr_billede.UploadetDato`, 
    `hbr_billede.UploadetAf`, 
    `hbr_billede.Fotograf`, 
    `hbr_billede.Caption`, 
    `hbr_billede.FeaturedFrontGallery`, 
    `hbr_billede.FeaturedWorldtour`, 
    `hbr_galleri.GalleriNavn`
FROM `hbr_billede` 
INNER JOIN `hbr_galleri`
ON `hbr_billede.GalleriID` = `hbr_galleri.GalleriID`
WHERE `hbr_billede.BilledeID` = 17 

我尝试过多种方法让它在phpMyAdmin中运行,但没有运气。有人能告诉我我做错了什么,还是指出了我的方向?

我的sql为这两个表创建脚本,如下所示:

CREATE TABLE hbr_galleri
(
    GalleriID int AUTO_INCREMENT NOT NULL,
    GalleriNavn varchar(255) NOT NULL,
    OprettetDato timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    DatoEvent date NOT NULL,
    AntalBilleder int,
    FeaturedBillede int,
    OprettetAf int NOT NULL, /* BrugerID*/
    Offentligt boolean NOT NULL DEFAULT true,
    unikt_galleri varchar(255) DEFAULT NULL,
    PRIMARY KEY (GalleriID),
    UNIQUE KEY unikt_galleri (GalleriNavn,DatoEvent),
    FOREIGN KEY (OprettetAf) REFERENCES hbr_bruger(BrugerID) /*ON DELETE CASCADE*/
);

CREATE TABLE hbr_billede
(
    BilledeID int AUTO_INCREMENT NOT NULL,
    GalleriID int NOT NULL,
    FilePath varchar(255) NOT NULL,
    FilePathThumb varchar(255) NOT NULL,
    UploadetDato timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UploadetAf int NOT NULL,
    Fotograf varchar(255) NOT NULL,
    Caption varchar(255),
    FeaturedFrontGallery boolean NOT NULL DEFAULT false,
    FeaturedWorldtour boolean NOT NULL DEFAULT false,
    PRIMARY KEY (BilledeID),
    FOREIGN KEY (GalleriID) REFERENCES hbr_galleri(GalleriID) ON DELETE CASCADE,
    UNIQUE (FilePath),
    UNIQUE (FilePathThumb)
);

1 个答案:

答案 0 :(得分:2)

您需要在INNER JOIN之前加上WHERE子句。您还需要修复反引号,例如

`hbr_billede.BilledeID`

应更改为此

`hbr_billede`.`BilledeID`

尝试将您的查询更改为此

SELECT 
    `hbr_billede`.`BilledeID`, 
    `hbr_billede`.`GalleriID`, 
    `hbr_billede`.`FilePath`, 
    `hbr_billede`.`FilePathThumb`, 
    `hbr_billede`.`UploadetDato`, 
    `hbr_billede`.`UploadetAf`, 
    `hbr_billede`.`Fotograf`, 
    `hbr_billede`.`Caption`, 
    `hbr_billede`.`FeaturedFrontGallery`, 
    `hbr_billede`.`FeaturedWorldtour`, 
    `hbr_galleri`.`GalleriNavn`
FROM `hbr_billede` 
INNER JOIN `hbr_galleri`
ON `hbr_billede`.`GalleriID` = `hbr_galleri`.`GalleriID`
WHERE `BilledeID` = 17