0

我有以下表格:

CREATE TABLE IF NOT EXISTS "image_e"
(
    "id"    int unsigned NOT NULL AUTO_INCREMENT,
    "title" varchar(32),
    "alt"   varchar(16)  NOT NULL,

    PRIMARY KEY ("id")

)   ENGINE = MyISAM DEFAULT CHARSET = utf8 COLLATE = utf8_bin;

CREATE TABLE IF NOT EXISTS "image_version_e"
(
    "id"      int unsigned      NOT NULL AUTO_INCREMENT,
    "imageId" int unsigned      NOT NULL,
    "format"  varchar(4)        NOT NULL,
    "size"    int unsigned      NOT NULL,
    "width"   smallint unsigned NOT NULL,
    "height"  smallint unsigned NOT NULL,
    "src"     varchar(64)       NOT NULL,

    PRIMARY KEY ("id")

)   ENGINE = MyISAM DEFAULT CHARSET = utf8 COLLATE = utf8_bin;

ALTER TABLE "image_version_e"
    ADD UNIQUE KEY ("imageId", "format", "width", "height"),
    ADD UNIQUE KEY ("src");

我想将这些加入表格以将图像行与其关联的图像版本行链接起来。我加入他们的顺序真的很重要吗?如果我这样做有什么区别:

SELECT *
FROM image_e
LEFT JOIN image_version_e
    ON image_version_e.imageId = image_e.id

代替 :

SELECT *
FROM image_version_e
LEFT JOIN image_e
    ON image_e.id = image_version_e.id
4

1 回答 1

1

You are doing left joins so there is a big difference between the two queries.

The first keeps all records in image_e along with matching records in image_version_e.

The second keeps all records in image_version_e along with matching record sin image_e.

Of these two, you should use the one that is semantically correct for your application.

It is quite possible that you really want an inner join:

SELECT *
FROM image_e inner join
     image_version_e
     ON image_version_e.imageId = image_e.id;

With an inner join, the join order shouldn't make a difference (the compiler will choose what it considers to be the best order).

于 2013-05-17T20:34:40.587 回答