mysql - 有人可以帮我按别名分组和处理空值吗?

标签 mysql group-by case

我想使用 50 页范围对页数进行排名。 我正在尝试根据页面范围显示我拥有的书籍 对页面计数进行排名。这意味着任何页数为 500-549 的书都具有相同的 秩;页数 550-599 具有相同的排名;页数 600-649 具有相同的排名; 页数 650-699 具有相同的排名等。我不能跳过排名数字。我该如何解决这个问题?

不确定这是否正确。这是我目前所拥有的:

    set @rownum:= 0;
select page_count, Ranking
from(
SELECT case 
when page_count between 0 and 49 then '0 - 49'
when page_count between 50 and 99 then '50 - 99'
when page_count between 100 and 149 then '100 - 149'
when page_count between 150 and 199 then '150 - 199'
when page_count between 200 and 249 then '200 - 249'
when page_count between 250 and 299 then '250 - 299'
when page_count between 300 and 349 then '300 - 349'
when page_count between 350 and 399 then '350 - 399'
when page_count between 400 and 449 then '400 - 449'
when page_count between 450 and 499 then '450 - 499'
when page_count between 500 and 549 then '500 - 549'
when page_count between 550 and 599 then '550 - 599'
when page_count between 600 and 649 then '600 - 649'
when page_count > 650 then 'Page Count over 650'
end as page_count
,
  @rownum:= @rownum + 1 as Ranking 
  from a_bkinfo.books
  )tbl
  group by page_count; 

为什么 50 -99 不合适? image

这是表格:

create table a_bkinfo.books (
    book_id           integer          not null
  , title             varchar(75)      not null 
  , publ_id           integer          null
  , year_publd        integer          not null
  , isbn              varchar(17)      null
  , page_count        integer          null  
  , list_price        numeric(6,2)     null  
  , constraint bk_books_pk             primary key (book_id)
  , constraint bk_books_publ_fk        foreign key(publ_id) 
               references a_bkinfo.publishers  (publ_id)
  , constraint book_id_range           check (book_id > 1000)
  , constraint bk_page_count_ck        check (page_count >= 0)
  , constraint bk_price_ck             check (list_price >= 0)             
  , constraint bk_books_year_ck        check (year_publd >= 1850)
)engine = INNODB;

  And here are the inserts:
-- books  
insert into a_bkinfo.books values (1101, 'Programming SQL Server with VB.NET',              9000, 2002, '0735615357',    300, 59.99);
insert into a_bkinfo.books values (1102, 'Practical Standards for VB.NET',                  9000, 2003, '0735613568',    250, 49.99);
insert into a_bkinfo.books values (1103, 'Selected Poems',                                  9456, 1949,  null,           125, 12.00);
insert into a_bkinfo.books values (1104, 'Sibley Guide to Bird Life and Behavior',          9102, 2001, '0679451234',    604, 45.00);
insert into a_bkinfo.books values (1105, 'SQL:1999 Relational Language Concepts',           9745, 2002, '1558604561',    450, 59.95);
insert into a_bkinfo.books values (1106, 'SQL for Smarties',                                9745, 1995, '1558603239',    250, 29.00);
insert into a_bkinfo.books values (1107, 'SQL Puzzles and Answers',                         9745, 1997, '1558604537',    325, 25.00);
insert into a_bkinfo.books values (1108, 'Database Systems',                                9325, 1996,  null,           680, 39.95);
insert into a_bkinfo.books values (1109, 'Intro to DB Systems-7th Ed',                      9325, 2000, '0201385902',    650, 80.00);
insert into a_bkinfo.books values (1110, 'Adv SQL:1999 Object_Relational Features',         9745, 2002, '1558606077',    520, 59.95);


insert into a_bkinfo.books values (1128, 'Temporal Data and the Relational Model',          9325, 2003, 'na',            275, 49.95);
insert into a_bkinfo.books values (1133, 'Leaves of Grass',                                 9623, 1902,  null,           125, 19.95);
insert into a_bkinfo.books values (1142, 'Relational Database Theory',                      9521, 1993,  null,           879, 95.00);
insert into a_bkinfo.books values (1161, 'SQL Programming Style',                           9745, 2005, '0120887975',    780, 35.00);
insert into a_bkinfo.books values (1162, 'Trees and Hierarchies',                           9745, 2004, '1558609202',    350, 35.00);
insert into a_bkinfo.books values (1180, 'MySQL Database Design and Tuning',                9825, 2005, '9780672234650', 400, 49.99);
insert into a_bkinfo.books values (1175, 'MySQL in a Nutshell',                             9822, 2008, '9780596514331', 538, 34.99);
insert into a_bkinfo.books values (1182, 'MySQL Cookbook',                                  9822, 2007, '9780596527082', 918, 49.99);
insert into a_bkinfo.books values (1185, 'MySQL Stored Procedures',                         9822, 2007, '9780596100896', 595, 49.99);
insert into a_bkinfo.books values (1184, 'MySQL Developer''s Library',                      9325, 2009, '9780672329388', 650, 49.99);
insert into a_bkinfo.books values (1301, 'ADO and Oracle Workbook',                         9000, 2002, '0265615357',      0, 59.99);
insert into a_bkinfo.books values (1302, 'ADO: the ebook',                                  9000, 2002, '0852515358',   null, 49.99);
insert into a_bkinfo.books values (1303, 'Rainbows and Rainbows',                           9521, 2002, '0657895157',   null, 59.99);
insert into a_bkinfo.books values (1304, 'Stories of Discoveries',                          9325, 2002, '0777788887',    300, 59.99);
insert into a_bkinfo.books values (1305, 'Journeys Through Flatland',                       9325, 1958, '0387515357',    100,  9.99);
insert into a_bkinfo.books values (1306, 'Myths of SQL',                                    9664, 2000, '0454615027',   2895,259.99);



insert into a_bkinfo.books values (1188, 'SQL for MySQL Developers',                        9325, 2007, '9780314973851', 105, 49.99);
insert into a_bkinfo.books values (1199, 'SQL is Fun',                                      null, 2007,  null,            98, 19.99);
insert into a_bkinfo.books values (2001, 'Programming SQL Server 2005',                     9822, 2006, '0596003216',    675, 49.99);
insert into a_bkinfo.books values (2002, 'SQL Server 2005 A Beginner''s Guide',             9030, 2006, '0072260939',    402, 39.99);
insert into a_bkinfo.books values (2003, 'SQL Server 2005 Developer''s Guide',              9030, 2006, '0072260998',    402, 49.99);
insert into a_bkinfo.books values (2004, 'SQL Server 2005 Stored Procedure Prg',            9030, 2006, '0072262888',    399, 59.99);
insert into a_bkinfo.books values (2005, 'Developer''s Guide to SQL Server 2005',           9325, 2006, '0321382188',    894, 59.99);
insert into a_bkinfo.books values (2006, 'T_SQL Programming (Inside series)',               9000, 2006, '9780756978',    390, 44.99);
insert into a_bkinfo.books values (2007, 'T_SQL Querying (Inside series)',                  9000, 2006, '9780733132',    391, 44.99);
insert into a_bkinfo.books values (2008, 'SQL Server 2005 T_Sql Recipies',                  9444, 2006, '159059570X',    503, 59.99);
insert into a_bkinfo.books values (2009, 'SQL Server 2005 Express Edition',                 9664, 2006, '0764589237',    150, 29.99);



insert into a_bkinfo.books values (1258, '.Net Development for Microsoft Office',           9000, 2005, '0735621322',    500, 49.99);
insert into a_bkinfo.books values (1689, 'Programming Visual Basic 2005: The Language',     9000, 2006, '9780735621831', 980, 59.99);
insert into a_bkinfo.books values (1678, 'Pro .NET 2.0 Windows Forms and Controls VB 2005', 9444, 2006, '1590959693',   1002, 49.99);
insert into a_bkinfo.books values (1278, 'Beginning VB 2008 Databases',                     9444, 2008, '9781590599471', 408, 44.99);
insert into a_bkinfo.books values (1478, 'Beginning OO Programming with VB 2005',           9444, 2006, '1590597695',    368, 44.99);
insert into a_bkinfo.books values (1894, 'Programming Visual Basic 2005',                   9822, 2005, '0596009496',    548, 39.99);
insert into a_bkinfo.books values (1279, 'Data-Driven Services with Silverlight 2',         9822, 2009, '9780596523091', 336, 44.99);
insert into a_bkinfo.books values (1776, 'Doing Objects in Visual Basic 2005',              9325, 2007, '9780321320490', 500, 49.99);
insert into a_bkinfo.books values (1948, 'Framework Design Guidelines',                     9325, 2006, '0321246756',    346, 44.99);
insert into a_bkinfo.books values (1077, 'Programming for Poets',                           9456, 2009, null,            401, 40.25);
insert into a_bkinfo.books values (1835, 'Data Binding with Windows Forms 2.0',             9325, 2006, '032126892X',    634, 49.99);
insert into a_bkinfo.books values (1541,  'Freethinkers: A History of American Secularism', 9023, 2004, '9780805077766', 448, 12.79);
insert into a_bkinfo.books values (1542,  'The Great Agnostic: Robert Ingersoll and American Freethought',  
                                                                                            9021, 2013, '9780300137255', 256, 16.99);
insert into a_bkinfo.books values (1543,  'Ties That Bind:The Story of an Afro-Cherokee Family in Slavery and Freedom',  
                                                                                            9024, 2006, '9780520250024', 327, 26.96);
insert into a_bkinfo.books values (1544,  'The House on Diamond Hill: A Cherokee Plantation Story',  
                                                                                            9024, 2012, '9780807872673', 336, 17.76);
insert into a_bkinfo.books values (1545,  'Team of Rivals: The Political Genius of Abraham Lincoln',  
                                                                                            9776, 2006, '9780739469767', 944, 13.96);
insert into a_bkinfo.books values (1546,  'The Johnstown Flood',                            9776, 1987, '9780671207144', 304, 10.39);


insert into a_bkinfo.books values (1677, 'Windows Forms 2.0 Programming',                   9325, 2006, '0321267966',    982, 74.99);
insert into a_bkinfo.books values (1670, 'Applied .NET Framework Programming VB.NET',       9000, 2003, '0735678772',    608, 49.99);
insert into a_bkinfo.books values (1401, 'Visual Studio Tools for Office',                  9325, 2006, '0321334884',    976, 54.99);
insert into a_bkinfo.books values (1537, 'The BedSide Book of Birds',                       9725, 2005, '0385514832',     68, 29.95);
insert into a_bkinfo.books values (1357, 'Why Birds Sing',                                  9725, 2005, '046507135X',    240, 26.00);
insert into a_bkinfo.books values (1609, 'In the Company of Crows and Ravens',              9725, 2005, '0300100760',    376, 18.95);
insert into a_bkinfo.books values (1979, 'Pro VB 2008 and the .NET 3.5 Platform',           9444, 2008, '9781590598221',1368, 59.99);
insert into a_bkinfo.books values (1457, 'Visual Basic 2008 Recipes',                       9444, 2008, '9781590599709', 300, 79.99);
insert into a_bkinfo.books values (1425, 'The Singing Life of Birds',                       9561, 2005, '0618405682',    468, 28.09);
insert into a_bkinfo.books values (1978, 'Acoustic Communication in Birds Vol1',            9561, 1983, '9780124268012', 360,103.91);


insert into a_bkinfo.books values (1621,  'The Unfeathered Bird',                           9020, 2013, '9780691151342', 304, 31.29);
insert into a_bkinfo.books values (1622,  'Bird Sense',                                     9020, 2012, '9780802779663', 265, 25.00);
insert into a_bkinfo.books values (1623,  'Lichens of North America',                       9021, 2001, '9780300082494', 828,135.00);
insert into a_bkinfo.books values (1624,  'Outstanding Mosses and Liverworts of Pennsylvania and Nearby States',             
                                                                                            9021, 2006, '9780976092575',9, 19.99);
insert into a_bkinfo.books values (1626,  'Bark: A Field Guide to Trees of the Northeast',  9021, 2011, '9781584658528', 280, 25.95);
insert into a_bkinfo.books values (1625,  'Winter Weed Finder: A Guide to Dry Plants in Winter (Nature Study Guides)',              
                                                                                            9021, 1989, '9780912550176',  64,  4.95);
insert into a_bkinfo.books values (1627,  'The Ants',                                       9022, 1990, '9780674040755', 732,120.18);
insert into a_bkinfo.books values (1628,  'The Superorganism:The Beauty, Elegance, Strangeness of Insect Societies',               
                                                                                            9902, 2008, '9780393067040', 544, 34.65);
insert into a_bkinfo.books values (1629,  'The Leafcutter Ants: Civilization by Instinct',  9022, 1990, '9780393338683', 160, 19.95);
insert into a_bkinfo.books values (1630,  'The Social Conquest of Earth',                   9022, 2012, '9780871404138', 352, 27.95);



insert into a_bkinfo.books values (1448, 'Backyard Birdsong Guide: Western North America',  9561, 2008, '9780811863971',3192, 29.99);
insert into a_bkinfo.books values (1877, 'High Performance MySQL',                          9822, 2008, '9780596101718', 708, 49.99);
insert into a_bkinfo.books values (1200, 'The Mismeasure of Man',                           9902, 1996, '9780393314250', 488, 17.95);
insert into a_bkinfo.books values (1245, 'A Scientific Approach to SQL Testing',            9902, 2010, '9780366214250', 488, 52.95);
insert into a_bkinfo.books values (1774, 'Ever Since Darwin',                               9902, 1992, '9780393308181', 288, 15.95);
insert into a_bkinfo.books values (1234, 'Hen''s Teeth and Horse''s Toes ',                 9902, 1994, '9780393311037', 416, 17.95);
insert into a_bkinfo.books values (1269, 'Querying XML',                                    9745, 2006, '9781558607118', 848, 63.95);
insert into a_bkinfo.books values (1525, 'Interface-Oriented Design',                       9725, 2006, '0976697050',    213, 29.99);
insert into a_bkinfo.books values (1619, 'The Oject-Oriented Thought Process',              9725, 2004, '9780672326110', 158, 29.99);
insert into a_bkinfo.books values (1483, 'Programming with XML',                            9745, 2008, null,            125, 19.99);
insert into a_bkinfo.books values (2017, 'Functional Programming',                          9528, 2010, '9781933988924', 528, 49.99);

insert into a_bkinfo.books values (2018,  'Oracle Database 11g SQL',                        9030, 2008, '9780071498500', 650, 49.99);
insert into a_bkinfo.books values (2025,  'Oracle SQL Fundamentals I Exam Guide',           9030, 2008, '9780071597869', 572, 59.99);
insert into a_bkinfo.books values (2027,  'Mastering Oracle SQL and SQL-Plus',              9444, 2005, '9781590594487', 464, 39.99);

insert into a_bkinfo.books values (2028,  'Mastering Oracle Databases',                     9444, 2010, '9781599594487', 464, 59.99);
insert into a_bkinfo.books values (2029,  'The Forgotten Bird Strikes Back ',               9030, 2010, '9091599594487',   5,  1.99);
insert into a_bkinfo.books values (2031,  'Comparative SQL',                                9444, 2013, '9781599591237', 750, 99.99);
insert into a_bkinfo.books values (2032,  'Oracle and the rest of the world',               9030, 2013, '9091599593217', 250, 55.99);
insert into a_bkinfo.books values (2622,  'Outstanding Bryophytes',                         9021, 2013, null,956, 89.99);
insert into a_bkinfo.books values (2623,  'Hornworts and Liverworts in your Garden ',       9021, 2013, null,501, 29.99);

最佳答案

  1. NULL 需要处理。所以请使用IS NULL

  2. 要为您的自定义范围创建订单,您必须使用 一个技巧。排序现在发生在 Character wise.. not number wise.. 所以 50在450之后..

简单的方法是让它固定格式..就像附加一个'0'..从'50'->'050'..你就完成了。 也用 LPAD '0' 将其设为 3 位数。我已经为您修改了查询。 !

  SET @rownum:= 0;
SELECT page_count, Ranking
from(
SELECT CASE
WHEN page_count BETWEEN 0 AND 49 OR page_count IS NULL THEN '000 - 049'
WHEN page_count BETWEEN 50 AND 99 THEN '050 - 099'
WHEN page_count BETWEEN 100 AND 149 THEN '100 - 149'
WHEN page_count BETWEEN 150 AND 199 THEN '150 - 199'
WHEN page_count BETWEEN 200 AND 249 THEN '200 - 249'
WHEN page_count BETWEEN 250 AND 299 THEN '250 - 299'
WHEN page_count BETWEEN 300 AND 349 THEN '300 - 349'
WHEN page_count BETWEEN 350 AND 399 THEN '350 - 399'
WHEN page_count BETWEEN 400 AND 449 THEN '400 - 449'
WHEN page_count BETWEEN 450 AND 499 THEN '450 - 499'
WHEN page_count BETWEEN 500 AND 549 THEN '500 - 549'
WHEN page_count BETWEEN 550 AND 599 THEN '550 - 599'
WHEN page_count BETWEEN 600 AND 649 THEN '600 - 649'
WHEN page_count >=650 THEN 'Page Count over 650'
END AS page_count
,
  @rownum:= @rownum + 1 AS Ranking
  FROM books
  )tbl
  GROUP BY page_count;

关于mysql - 有人可以帮我按别名分组和处理空值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20580109/

相关文章:

php - 重复的动态链接

mysql:为什么左连接不使用索引?

apache-spark - 如何将不同的聚合函数应用于同一列为什么要对 spark 数据帧进行分组?

Mysql 查询 - 显示产品的第二个分类法阻止了我

C# if else vs if 或 vs switch case

c++ - "A case label may only be used within a switch"

php - Laravel:创建动态属性

mysql - 当有更新触发器时,重复键更新不起作用

mysql sum group by month and date 使用契约(Contract)开始和结束日期

mysql - 如何根据mysql中的两列日期求和金额