简体   繁体   English

MySQL选择名称按字母分组,每个字母n个结果

[英]MySQL select names group by alphabet limit to n results per letter

I want to create a query that will get the names from one table then joint it with others, ordered alphabetically for each letter of the alphabet then limit to n results for each letter in one query. 我想创建一个查询,该查询将从一个表中获取名称,然后将其与其他表合并,按字母顺序对字母的每个字母排序,然后在一个查询中将每个字母的结果限制为n个。

So I don't wan't running 26 queries for each letter like: 因此,我不会为每个字母运行26个查询,例如:

SELECT CONCAT_WS(' ',first_name,last_name) AS name FROM encyclopedia WHERE name LIKE "A%" ORDER BY rand() LIMIT 4 
SELECT CONCAT_WS(' ',first_name,last_name) AS name FROM encyclopedia WHERE name LIKE "B%" ORDER BY rand() LIMIT 4 
SELECT CONCAT_WS(' ',first_name,last_name) AS name FROM encyclopedia WHERE name LIKE "C%" ORDER BY rand() LIMIT 4

Right now I have: 现在我有:

SELECT 
      e.id,
      CONCAT_WS(' ',first_name,last_name) AS name,
      sg.name AS sub_name, 
      sg.id AS sub_id 
      FROM ( SELECT id,first_name,last_name FROM encyclopedia ORDER BY RAND() LIMIT 4) AS e
      LEFT JOIN encyclopedia_subgenres esub
      ON ( e.id = esub.enc_id )
      LEFT JOIN subgenres sg
      ON ( esub.subgenre_id = sg.id )
      ORDER BY name

Then from php I create the array from the result: 然后从PHP我从结果创建数组:

    Array
(
    [b] => Array
        (
            [2] => Array
                (
                    [name] => B Style
                    [genres] => Array
                        (
                            [26] => Classic
                            [27] => X
                            [29] => Y
                        )

                )

            [7] => Array
                (
                    [name] => Beyonce Giselle Knowles
                    [genres] => Array
                        (
                            [32] => Pop
                        )

                )

        )

    [i] => Array
        (
            [1] => Array
                (
                    [name] => Ivan D
                    [genres] => Array
                        (
                            [27] => R&B
                            [2] => Jazz
                        )

                )

        )

    [m] => Array
        (
            [3] => Array
                (
                    [name] => Maria T
                    [genres] => Array
                        (
                            [26] => Classical
                            [27] => Pop
                            [28] => Dance
                        )

                )

        )

)

But the thing is that it limits me to only 4 random results which can be 4 on the same letter or any combination. 但事实是,它限制了我只有4个随机结果,在同一字母或任意组合上可以是4。 Is there a way in doing this ? 有办法吗?

Thanks. 谢谢。

I think sql is not that much flexible - but i am not sure. 我认为sql不太灵活-但我不确定。 You have to write stored-procedures for that. 您必须为此编写存储过程。

First create one that generate this sequence: A, B, C, ..., Z, A, B, C, ..., Z, A, B, C, ... lets call it magic_sequence. 首先创建一个生成此序列的序列:A,B,C,...,Z,A,B,C,...,Z,A,B,C,...让我们称其为magic_sequence。

Then create a temporary table having a column, init, seeded from magic sequence, another, count_down, be equal to 4. It should have 26 rows. 然后创建一个临时表,该表具有一列init,该列是从魔术序列开始的,另一列count_down等于4。它应该有26行。 lets call it init_table; 我们称它为init_table;

Then write another function namely acceptance_test that 然后写另一个函数,acceptance_test

- take a string S with
- decrease count_down of the corresponding row in init_table
- return true if count_down > = 0; otherwise false

now have a query like that: 现在有这样的查询:

SELECT CONCAT_WS(' ',first_name,last_name) AS name FROM encyclopedia WHERE acceptance_test(name) LIMIT 4*26

You also have to randomize if you want. 如果需要,还必须随机化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM