博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql按中文数字排序
阅读量:5016 次
发布时间:2019-06-12

本文共 2355 字,大约阅读时间需要 7 分钟。

 

 

有表4张

建表和插入数据sql

DECLARE @p_Building TABLE    (      id INT ,      BidName NVARCHAR(20)    );DECLARE @p_Room TABLE    (      id INT ,      RoomNo INT ,      RoomArea INT ,      Bidid INT    );DECLARE @p_Customer TABLE    (      id INT ,      CstName NVARCHAR(20) ,      CstTel VARCHAR(20) ,      CstSex NVARCHAR(5)    );DECLARE @p_Cst2Room TABLE    (      id INT ,      RoomID INT ,      CstID INT ,      CstNO INT    );INSERT  INTO @p_Building( id, BidName ) VALUES  ( 1, N'四栋' );INSERT  INTO @p_Building( id, BidName ) VALUES  ( 2, N'一栋' );INSERT  INTO @p_Building( id, BidName ) VALUES  ( 3, N'二栋' );INSERT  INTO @p_Building( id, BidName ) VALUES  ( 4, N'三栋' );INSERT INTO @p_Room( id, RoomNo, RoomArea, Bidid ) VALUES  ( 1, 101, 80, 1)INSERT INTO @p_Room( id, RoomNo, RoomArea, Bidid ) VALUES  ( 2, 102, 80, 2)INSERT INTO @p_Room( id, RoomNo, RoomArea, Bidid ) VALUES  ( 3, 103, 99, 3)INSERT INTO @p_Room( id, RoomNo, RoomArea, Bidid ) VALUES  ( 4, 104, 87, 4)INSERT INTO @p_Customer( id, CstName, CstTel, CstSex )VALUES  ( 1,N'张三','',N'')INSERT INTO @p_Customer( id, CstName, CstTel, CstSex )VALUES  ( 2,N'李四','',N'')INSERT INTO @p_Customer( id, CstName, CstTel, CstSex )VALUES  ( 3,N'王五','',N'')INSERT INTO @p_Customer( id, CstName, CstTel, CstSex )VALUES  ( 4,N'赵六','',N'')INSERT INTO @p_Cst2Room( id, RoomID, CstID, CstNO )VALUES  ( 1,1,1,1)INSERT INTO @p_Cst2Room( id, RoomID, CstID, CstNO )VALUES  ( 2,1,2,2)INSERT INTO @p_Cst2Room( id, RoomID, CstID, CstNO )VALUES  ( 3,1,3,3)INSERT INTO @p_Cst2Room( id, RoomID, CstID, CstNO )VALUES  ( 4,1,4,4)

要得到下面结果:

执行以下sql:

SELECT  A.BidName AS [楼栋名称] ,        B.RoomNo AS [房号] ,        ( SELECT    C.CstName + ','          FROM      @p_Customer AS C                    INNER JOIN @p_Cst2Room AS D ON C.id = D.CstID                                                   AND D.RoomID = B.id        FOR          XML PATH('')        ) AS [客户名称] ,        B.RoomArea AS [房间面积]FROM    @p_Building AS A        INNER JOIN @p_Room AS B ON A.id = B.Bidid; --AND A.id=1

要得到以下结果

SELECT  A.BidName AS [楼栋名称] ,        CHARINDEX(SUBSTRING(A.BidName, 1, 1), '一二三四五六七八九十') AS OrderNum ,        B.RoomNo AS [房号] ,        B.RoomArea AS [房间面积]FROM    @p_Building AS A        INNER JOIN @p_Room AS B ON A.id = B.BididORDER BY CHARINDEX(SUBSTRING(A.BidName, 1, 1), '一二三四五六七八九十');

 

转载于:https://www.cnblogs.com/zhyue93/p/sql_sort.html

你可能感兴趣的文章
【同行说技术】JavaScript开发的资源总结和心灵鸡汤
查看>>
IE6中正确显示png图片
查看>>
highcharts时间图
查看>>
c#冒泡法排序
查看>>
xeno 实时性能测试 系统时钟1秒100个tick再测试
查看>>
window 8 metro 访问Assets文件
查看>>
解读刘强东关于人才的两个标准和5个层次
查看>>
iOS9下UICollectionViewCell的awakeFromNib问题
查看>>
厚积薄发系列之JDBC详解
查看>>
js面向对象-原型链
查看>>
ThinkPHP入门
查看>>
【Google SDK】 Android 密钥库系统02_两种使用方式
查看>>
poj 3140
查看>>
CentOS上PHP完全卸载
查看>>
2016huasacm暑假集训训练四 递推_B
查看>>
成长这件小事
查看>>
重启部署在阿里云上的huginn
查看>>
music21 关联 MuseScore 和 Lilypond
查看>>
【hdu3579-Hello Kiki】拓展欧几里得-同余方程组
查看>>
自学stm32就要记住入了这个“大坑”要耐得住寂寞
查看>>