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

有两个记录urun_id = 17。我只想返回一条记录。我有category, products,images表。用户在images表中添加了 2 张照片urun_id=17

但我只想显示一条在sira.

如果查询的输出是

  • 25~个~3个
  • 17~电话~2
  • 17~电话~4

然后,我想返回

  • 17~电话~2
4

2 回答 2

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 回答
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 回答