1

表1

cid
itemdesc
itemprice

表2

cid
imagename
status

私の最初のテーブルには一意のCIDがあります(重複はありません)table2にLEFT JOINしたいのですが、CIDごとに複数の行があります

cid      imagename           status
1        image1-of-cid1      test1
1        image2-of-cid1      test2
2        image1-of-cid2      test3
2        image2-of-cid2      test4
2        image3-of-cid2      test5

しかし、クエリがテーブル1の各レコードの最初の行のみを返すようにしたいだけです。

ありがとう

4

4 に答える 4

2

上記のジョン・ウーの答えに同意します。テーブル2の最初の行を実際に取得するには、ある種のサブクエリが必要です。

SELECT
t1.[id],
t2.*
FROM table1 AS t1
LEFT JOIN table2 AS t2
    ON t2.cid = (SELECT TOP 1 cid FROM table2 WHERE cid = t1.cid)
于 2012-11-07T08:19:25.573 に答える
1

imagenameごと に1つ取得する追加のサブクエリを作成する必要がありますcid。これを試して、

SELECT  a.*, b.*
FROM    table1 a
        LEFT JOIN
        (
            SELECT  cid, MIN(imagename) minImage
            FROM table2
            GROUP BY cid
        ) c ON a.cid = c.cid 
        LEFT JOIN table2 b
            ON  c.cid = b.cid  AND
                b.imageName = c.minImage
于 2012-11-07T07:42:43.100 に答える
0
Select 
    distinct a.cid,a.itemdesc,b.imagename,a.itemprice,b.status
from table1 a,
table2 b
where a.cid=b.cid
于 2012-11-07T07:43:10.427 に答える
0

これを試して:

SELECT a.cid, a.itemdesc, a.itemprice, b.imagename, b.status 
FROM table1 a 
LEFT OUTER JOIN table2 AS b ON a.cid = b.cid 
GROUP BY a.cid, a.itemdesc, a.itemprice;
于 2012-11-07T08:45:30.833 に答える