0

我通过以下方式在表格中有一些联系人数据。

Contact_Name              Contact Details

Acting Generals Office      ABCD
BlackBerryOffice                A1B1
BBM Help                        X1Y1
Customer Care               A2B2
D_Link Routers              A3B3

现在在一个页面中,我必须在我的ASP.NET 页面中以这种方式显示数据- (每个条目的第一个字符以及具有该字符的所有条目)

一个

代理将军办公室

BlackBerryOffice BBM 帮助

C

客户服务

D

D_Link 路由器

请参阅下图了解更多详情。 在此处输入图像描述

单击 Acting Generals Office、BlackBerryOffice、Customer Care、D_Link Routers 将重定向到一个新页面,其中将显示相应的contact_details。(通过点击代理将军办公室以下将打开)

Acting Generals Office      ABCD

请帮我解决一下这个。

4

1 回答 1

0

Could you provide sample output that you want to get? Because it's hard to tell what you really need by looking at your question. For now, try this and provide feedback if it is what you are looking for:

CREATE TABLE contacts (
  contact_name VARCHAR2(40),
  contact_details VARCHAR2(20)
);

INSERT INTO contacts VALUES ('Acting Generals Office', 'ABCD');
INSERT INTO contacts VALUES ('BlackBerryOffice', 'A1B1');
INSERT INTO contacts VALUES ('Customer Care', 'A2B2');
INSERT INTO contacts VALUES ('D_Link Routers', 'A3B3');

COMMIT;

-- show all letters, even when there are no contacts belonging to any of them
WITH letters AS (
  SELECT CHR(65 + level - 1) AS letter
    FROM dual
  CONNECT BY level <= ASCII('Z') - ASCII('A') + 1
)
SELECT lt.letter, ct.contact_name
  FROM letters lt
    LEFT JOIN contacts ct ON (lt.letter = UPPER(SUBSTR(ct.contact_name, 1, 1)))
ORDER BY 1, 2
;

Output:

LETTER CONTACT_NAME                           
------ ----------------------------------------
A      Acting Generals Office                   
B      BlackBerryOffice                         
C      Customer Care                            
D      D_Link Routers                           
E                                               
F                                               
.
.
.
Z           

-- Output only those letters where there are associated contacts

SELECT SUBSTR(contact_name, 1, 1), contact_name
  FROM contacts
ORDER BY 1, 2
;

Output:

SUBSTR(CONTACT_NAME,1,1) CONTACT_NAME                           
------------------------ ----------------------------------------
A                        Acting Generals Office                   
B                        BlackBerryOffice                         
C                        Customer Care                            
D                        D_Link Routers                           

Try at on SQLFiddle: SQLFiddle example

于 2013-10-27T12:58:41.777 回答