2012-01-11 48 views
1

是否可以在MySQL中選擇一行,然後錯過4行,然後錯過3行然後錯過5行,然後循環直到表的結尾?以不同的時間間隔選擇多行

我已經在網上找到了一些LIMIT和OFFSET教程,但它們工作正常,但只適用於一套。我需要多個。

我目前有這個設置工作在PHP中,但我覺得我的PHP代碼臃腫,因爲我只知道實現這一點的基本方法。

我的PHP代碼如下

$int_count = 0; 
$result = mysql_query("SELECT * FROM guitar_tunings_chords WHERE note_id >= '27'"); 
while ($row = mysql_fetch_array($result)) { 

    if ($int_count == 0)$n_1 = ($row["note"]); 
    if ($int_count == 4)$n_2 = ($row["note"]); 
    if ($int_count == 7)$n_3 = ($row["note"]); 
    if ($int_count == 12)$n_4 = ($row["note"]); 
    if ($int_count == 16)$n_5 = ($row["note"]); 
    if ($int_count == 19)$n_6 = ($row["note"]); 
    if ($int_count == 24)$n_7 = ($row["note"]); 
    if ($int_count == 28)$n_8 = ($row["note"]); 
    if ($int_count == 31)$n_9 = ($row["note"]); 
    if ($int_count == 36)$n_10 = ($row["note"]); 
    if ($int_count == 40)$n_11 = ($row["note"]); 
    if ($int_count == 43)$n_12 = ($row["note"]); 
    if ($int_count == 48)$n_13 = ($row["note"]); 
    if ($int_count == 52)$n_14 = ($row["note"]); 

    $int_count++; 

    } 

我所試圖做的是從大尺度只選擇大三和絃音符。

我需要能夠從表中選擇一個基本音符,然後選擇3個不同的間隔來創建和絃。


更新

  $result2 = mysql_query("SELECT * 
      FROM `guitar_tunings_links` 
      JOIN guitar_tunings_chords ON guitar_tunings_links.".$funtion_string." = guitar_tunings_chords.note 
      WHERE tuning = '".$chrd_tn."'") or die(mysql_error()); 

       while ($row = mysql_fetch_array($result2)) { 
        $bridge_note = ($row["note_id"]);   
       }/* 
       var_dump ($key_note); 
       var_dump ($bridge_note); 
       var_dump ($funtion_string); 
       var_dump ($chrd_tn);*/ 
        // SELECT * FROM `guitar_tunings_chords` WHERE `note_id` >= 28 LIMIT 0,8 
       $result4 = mysql_query("SELECT * FROM `guitar_tunings_chords` WHERE `note_id` >= ".$bridge_note." LIMIT ".$tuning_capo.",8") or die(mysql_error()); 
       while ($row = mysql_fetch_array($result4)) { 

        //Notes 
        $note = ($row["note"]); 

        // Replace # with z 
        $note = str_replace("#", "z", $note); 

        // Distinguish nut notes on or off 
        if (preg_match("/\b".$note."\b/i", $notes_array)) { 
         $n_nut_style = 'note_nut_on'; 
        } else { $n_nut_style = 'note_nut_off'; 
        } 

        // Distinguish fretboard notes on or off 
        if (preg_match("/\b".$note."\b/i", $notes_array)) { 
         $n_style = 'note_on'; 
        } else { $n_style = 'note_off'; 
        } 
+0

我知道PHP,但我不知道吉他和絃。我認爲你應該詳細說明這一部分。 – Hubro 2012-01-11 19:49:25

+0

您可以更改偏移量並再次發送查詢 – 2012-01-11 19:51:03

+0

1.您應該更喜歡一個*數組*或其他一些複合數據類型,而不是索引後綴變量。例如''array_push($ notes,$ row [「note」])''。 2.您應明確訂購該查詢,而不是依賴引擎無意中挑選您想要的訂單,即使這恰好是「隱含確定性事故」。 – pilcrow 2012-01-11 20:42:04

回答

2

你必須將其應用到您的確切表,但它應該工作相當不錯。

select * 
    from (select @i := @i+1 as count, 
       gtc.* from guitar_tunings_chords gtc 
     where note_id >= 27) counts 
    join (select @i := -1 as counter) dummy 
where count % 12 = 0 
    or (count-4) % 12 = 0 
    or (count-7) % 12 = 0; 

%12給出跳過的一個完整週期,-4和-7(和-0)是內部每個週期中的偏移量。

+0

+1用於模擬ROW_NUMBER(),並將模式設置爲所需行的方式,正如[@Wrikken也建議](http://stackoverflow.com/questions/8825605/select -multiple-rows-at-different-intervals/8825828#comment11017620_8825828) – pilcrow 2012-01-11 20:52:43

+1

ROW_NUMBER真的應該是MySQL中的本地函數 – 2012-01-11 21:00:24

+1

+1。值得一提的是'@ i'初始化可以放在派生表中:'... JOIN(SELECT @i:= -1 AS「counter」)dummy ......'我提出它,因爲接下來的問題幾乎總是,「太好了,但我怎麼在一個查詢中做到這一點「:) :) – pilcrow 2012-01-11 21:04:01

0

試試這個...我還沒有嘗試過自己,但在我的腦海它應該工作:)

$row_count = 0; 
$result = mysql_query("SELECT * FROM guitar_tunings_chords WHERE note_id >= '27'"); 
$notes = array(); // Notes array 
$pattern = array(4,3,5); // Pattern to skip notes 
$i = 0; // For pattern position 
$first = true; 
while ($row = mysql_fetch_array($result)) { 
    if($first) { // Add the first note 
     $notes[] = $row["note"]; 
     $first = false; 
    } else { 
     if($row_count == $pattern[$i]) { 
      // Add note 
      $notes[] = $row["note"]; 
      $i++; // Change pattern position 
      // Jump to start of pattern 
      if($i == 3) 
       $i = 0; 
      // Reset count 
      $row_count = -1; 
     } 
    } 
    $row_count++; 
} 
+0

我確實嘗試過這一個。但我希望我需要將每個選定的音符添加到一個變量中,然後我可以應用一個樣式來指示是否打開或關閉。所有的筆記都需要打印出來,我只需要指出哪些筆記是在打開的。我正在處理的文件在這裏.. – warmwhisky 2012-01-11 22:23:12

+0

http://www.gtdb.org/tuning_links/chord_generator/chords.php – warmwhisky 2012-01-11 22:23:26

+0

感謝您的回覆!它看起來不錯,因爲我可以做所有的間隔添加轉換Excel單元格之間的逗號。 – warmwhisky 2012-01-11 22:24:43

0

我會考慮可能使用使用間隔(0,4,7,12,16,19等)的chord_note表並將其用作SELECT的一部分,將初始note_id值(27)+ chord_note表中的條目加入

SELECT * 
    FROM guitar_tunings_chords 
WHERE note_id IN (SELECT 27+chord_interval 
         FROM chord_note 
       ) 

獲得更加稀奇,你甚至可以延長chord_note表來保存不同規模不同inteval細節,只是修改再選擇

+0

它假定有意義的ID,但如果它們是,它就會工作(I'儘管..) – Wrikken 2012-01-11 20:07:21

+1

儘管沒有子選擇,它可能是:WHERE id - 27> = 0 AND(((id - 27)%12)= 4 OR((id- 27)%12)= 7 OR(id - 27)%12 = 0)限制14' – Wrikken 2012-01-11 20:10:47

+0

感謝您的時間馬克。這當然是我現在正在考慮的事情。雖然我實際上並不瞭解你的SQL查詢,但我認爲只有內部區間的新表纔是實現這一目標的好方法。我上面的代碼只用於一個和絃。我目前正在處理這個文件http://www.gtdb.org/tuning_links/chord_generator/chords.php – warmwhisky 2012-01-11 22:27:05