以不同的间隔选择多行

发布于 2024-12-26 17:42:23 字数 2799 浏览 1 评论 0原文

是否可以在 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'; 
                    }

Is it possible to select one row in MySQL and then miss 4 rows and then miss 3 rows and then miss 5 rows and then loop this until the end of the table?

I have found some LIMIT and OFFSET tutorials online, they work but, are only good for one off set. I need multiple.

I currently have this setup to work in PHP but I feel my PHP code is bloated because I only know a basic way of achieving this.

My PHP code is as follows

    $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++;   

    }

What I am trying to do is select only Major triad notes from the Major scale.

I need to be able to select a base note from a table and then select at 3 different intervals to create chords.


Update

            $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'; 
                    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独夜无伴 2025-01-02 17:42:23

您必须将其应用到您的确切表格中,但它应该工作得很好。

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)是每个周期内的内部偏移量。

You have to apply this to your exact tables but it should work pretty good.

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 gives one complete cycle of skips, the -4 and -7 (and -0) is the internal offset within each cycle.

清浅ˋ旧时光 2025-01-02 17:42:23

试试这个......我自己没有尝试过,但在我看来它应该有效:)

$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++;
}

Try this...I haven't tried it myself but in my head it should work :)

$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++;
}
别理我 2025-01-02 17:42:23

我会考虑使用带有间隔(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表来保存不同音阶的不同间隔细节,只需修改子选择

I'd consider possibly using a chord_note table with the intervals (0,4,7,12,16,19, etc) and using that as part of your SELECT, adding the initial note_id value (27) + the entry in the chord_note table

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

Getting more fanciful, you could even extend the chord_note table to hold different inteval details for different scales, just modifying the subselect

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文