我有一个包含950行数据的字典数据库。我想按字母顺序按字母顺序显示单词列表。
对于示例表,a显示数据的第一(5)个单词从A:
开始A
-------------
Abacus
Abelian group
Abscissa
Absolute Value
Abstract Number
B
---------------
Bar Graph
Base
Base Depth of the Triangular Prism
Base of the Triangular Prism
Basic arithmetic operations
.
.
.
Z
---------------
Z-Intercept
Zero
Zero Divisors
Zero Element
Zone
每5个数据的获取表应该停止当前字母并跳转到下一个字母。
我不知道该怎么做。
我的表结构是
-------------------------
Name | Type
-------------------------
Id | int(3)
Word | varchar(45)
-------------------------
答案 0 :(得分:0)
答案 1 :(得分:0)
我尝试了下面的查询,现在限制为3。
简而言之,我们要做的是 - 按排序字词分组并限制为n
word_table
CREATE TABLE `offers`.`word_table` ( `Id` INT(3) NOT NULL , `Word` VARCHAR(45) NOT NULL ) ENGINE = InnoDB;
为所需输出编写的INSERT INTO `word_table` (`Id`, `Word`) VALUES ('1', 'Abacus'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('2', 'Abelian group'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('3', 'Abscissa'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('4', 'Absolute Value'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('5', 'Abstract Number'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('6', 'Bar Graph'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('7', 'Base'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('8', 'Base Depth of the Triangular Prism'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('9', 'Base of the Triangular Prism'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('10', 'Basic arithmetic operations'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('11', 'Z-Intercept'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('12', 'Zero'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('13', 'Zero Divisors'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('14', 'Zero Element'); INSERT INTO `word_table` (`Id`, `Word`) VALUES ('15', 'Zone');
查询
SELECT x.*
FROM (SELECT t.*,
CASE
WHEN @category != substring(t.Word,1,1) THEN @rownum := 1
ELSE @rownum := @rownum + 1
END AS rank,
@category := substring(t.Word,1,1) AS var_category
FROM word_table t
JOIN (SELECT @rownum := NULL, @category := '') r
ORDER BY t.Word) x
WHERE x.rank <= 3
ORDER BY `x`.`var_category` ASC
<强>输出强>
希望这可以帮助你!!!
答案 2 :(得分:0)
您可以使用单个查询来获取不同的第一个字符的结果以及使用此字母作为起始字母的所有记录
SELECT lower(left(filed_name,1)) as letter, group_concat(filed_name) as all_records FROM `table_name` where lower(left(filed_name,1)) in(SELECT DISTINCT LEFT(filed_name, 1) FROM table_name order by filed_name) group by lower(left(filed_name,1)) ORDER BY filed_name ASC