i çoğaltılması olmadan mysql seçim olamaz nasıl

3 Cevap php

benim sorunum çok karmaşık

Şimdi i files tablo var

I selected from it the files which added in last seven days. it returns many ids like 1,2,3,4,5 ok it's great. Now the problem:

i var tutorial tablosu

Her tutorial files tablonun bir dosya vardır

şimdi ben 7 gün eklenen son hangi files seçilen

and make list ordered by files downloaded count desc and after that I select from tutorial which equal tutorial id in the files table this mean

files_id tutorial_id
   1          2

files_id tutorial_id
   2          2

Bu bir eğitimde iki dosya demek

Şimdi öğretici

tut_id tut_title
 1        tite test

dosyalar listesinde son yedi gün içinde indirilebilir

file_id = 1 this mean tutorial id = 2
file_id = 2 this mean tutorial id = 2

This means I have one tutorial and two files in the most downloaded list will duplicate the tutorial title twice.

onun gerçekten karmaşık.

3 Cevap

Ben doğru anlamak, temelde tutorials en files en çok indirilenler karşı indirilen listelemek istiyorum.

Bu durumda, size TOP 10 almak için aşağıdaki sorguyu kullanabilirsiniz:

SELECT tutorials.tut_id,
       tutorials.tut_title,
       MAX(files.file_downloads) AS tut_downloads
  FROM tutorials, tutorial_files, files
  WHERE tutorials.tut_id = tutorial_files.tut_id
    AND files.file_id    = tutorial_files.file_id
  GROUP BY tutorials.tut_id, tutorials.tut_title
  ORDER BY tut_downloads DESC
  LIMIT 10;

Sen SUM Eğer başına öğretici indirme sayıları hesaplamak istiyorum nasıl bağlı bir tarafından MAX değiştirebilirsiniz.

Aynı, ancak son 7 gün eklenen dosyaları sınırlar:

SELECT tutorials.tut_id,
       tutorials.tut_title,
       MAX(files.file_downloads) AS tut_downloads
  FROM tutorials, tutorial_files, files
  WHERE tutorials.tut_id = tutorial_files.tut_id
    AND files.file_id    = tutorial_files.file_id
    AND files.file_added >= DATE_SUB(NOW(), INTERVAL 7 DAYS)
  GROUP BY tutorials.tut_id, tutorials.tut_title
  ORDER BY tut_downloads DESC
  LIMIT 10;

Eğer istediğiniz bu değilse, ben üzgünüm. Sorunuz çok belirsizdir.

Limit 1 sipariş sonra en üst satırı almak için kullanın. Bu (bunu sipariş unutmayın, bu yüzden en dosya indirme üstünde olacak) en dosya indirme olduğu, sadece ilk satır döndürür.