1
SELECT c.urun_id, a.kat_adi, b.resim_yolu, b.sira, 
c.urun_sira, c.urun_adi
FROM kategoriler a,resimler b, urunler c 
where  a.kat_id=c.kat_id and c.urun_id=b.urun_id
order by c.urun_sira,b.sira

との記録が2件ありますurun_id = 17。レコードを 1 つだけ返したい。categoryproductsimagesテーブルがあります。ユーザーが のimages表に2 枚の写真を追加しましたurun_id=17

しかし、最小値を持つレコードを 1 つだけ表示したいのですsira

クエリの出力が

  • 25~pc~3
  • 17~電話~2
  • 17~電話~4

じゃあ帰りたい

  • 17~電話~2
4

2 に答える 2

0

SQLServer2005+ ではROW_NUMBER関数を使用します

 ;WITH cte AS
 (
  SELECT c.urun_id, a.kat_adi, b.resim_yolu, b.sira, 
         c.urun_sira, c.urun_adi, 
         ROW_NUMBER() OVER (PARTITION BY c.urun_id ORDER BY b.sira) AS rn
  FROM kategoriler a JOIN urunler c ON a.kat_id=c.kat_id
                     JOIN resimler b ON c.urun_id=b.urun_id
  )
  SELECT *
  FROM cte
  WHERE rn = 1
  ORDER BY urun_sira, sira

他の方法

  SELECT c.urun_id, a.kat_adi, b.resim_yolu, b.sira, 
         c.urun_sira, c.urun_adi         
  FROM kategoriler a JOIN urunler c ON a.kat_id=c.kat_id
                     JOIN resimler b ON c.urun_id=b.urun_id  
  WHERE EXISTS (
                SELECT 1
                FROM resimler r
                WHERE b.urun_id = r.urun_id
                HAVING MIN(r.sira) = b.sira
                )
  ORDER BY c.urun_sira, b.sira
于 2013-02-10T00:41:53.907 に答える
0

あなたの質問を正しく理解していれば、これでうまくいくはずです。

SELECT c.urun_id, a.kat_adi, b.resim_yolu, min(b.sira),
       c.urun_sira, c.urun_adi
FROM kategoriler a,resimler b, urunler c
where a.kat_id=c.kat_id
and c.urun_id=b.urun_id
group by c.urun_id, a.kat_adi, b.resim_yolu,
       c.urun_sira, c.urun_adi
order by c.urun_sira,b.sira
于 2013-02-09T12:05:31.907 に答える