0

SQL文に問題があります。1 つのカテゴリ (エンティティ) に属するエンティティ ギャラリーがあります。次に、エンティティ GalleryImages があり、特定のギャラリーの画像が保存されています。ギャラリーには多くの画像を含めることができますが、画像なしでギャラリーを存在させることもできます (画像はまだ追加されていません)。

ここで、1 つ以上の画像があり、あるカテゴリに属する​​ (DESC) ギャラリーを選択するための SQL クエリを作成したいと考えています。

カテゴリ (id、名前)

ギャラリー (id、name、category_id)

GalleryImages (id, gallery_id, パス)

カテゴリ -> ギャラリー (1 対多) ギャラリー -> GalleryImages (1 対多)

4

2 に答える 2

0

うーん - 基本的な質問はすでに回答されているようです。しかし、あなたのセットアップは私を少し悩ませています...
私はあなたのテーブルを少し再構築すると思います(国際化が問題ではないと仮定します)。とりわけ、「カテゴリー」と「ギャラリー」の概念に根本的な違いはないと思います。現在、「スーパー」および「サブ」カテゴリ/ギャラリーが存在する場合があり、画像が一度に複数に属する場合があります (つまり、レニアサンス、ミケランジェロ、彫刻など)。タグの概念に似ています。

私はおそらくあなたのテーブルを次のように修正します:

Gallery (or Category, if you prefer)
=============
id  -- autoincrement
name  -- varchar(50) or something, unique
parent  -- fk reference to another Gallery.id row, optional

Image 
==========
id  -- autoincrement
name  -- varchar(50) or similar, non-unique
path  -- store as URI/URL, unique
description  -- varchar(128) or similar

Gallery_Image
===============
galleryId  -- fk reference to Gallery.id
imageId  -- fk reference to Image.id
         -- the pair is unique

Related_Gallery  -- optional table
================
galleryId  -- fk reference to Gallery.id
relationship  -- code, or fk reference to other table
relatedId  -- fk reference to Gallery.id
           -- entire row should be unique
           -- somewhat tricky to use.
于 2012-04-09T20:00:19.120 に答える
0

これを試して:

SELECT *
FROM Gallery as G
WHERE 
    G.category_id = 1
    AND G.id in (SELECT gallery_id FROM GalleryImages )

1 を選択したい caregory_id に置き換えます。

于 2012-04-09T19:42:52.677 に答える