2012-01-11 74 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