简体   繁体   English

Dropdownlist中的SQL联合之间的文本-ASP.net C#

[英]Text between SQL unions in Dropdownlist - ASP.net c#

I have a SQL query which brings back staff which are most important and should appear at top of the dropdown list, then all other staff below this. 我有一个SQL查询,该查询带回了最重要的人员,这些人员应出现在下拉列表的顶部,然后是此下方的所有其他人员。

The query is built using unions to collect both queries together. 该查询使用联合来构建,以将两个查询一起收集。

I would like to have text at the top of the dropdownlist and text in between the unions. 我想在下拉列表的顶部放置文本,在工会之间添加文本。 Is this possible? 这可能吗?

EG "Preferred Staff Members" as the top and "All Other Staff members" to separate the two unions EG“首选工作人员”为最高职位,“其他所有工作人员”将两个工会分开

select DISTINCT e.EMPLOY_REF, e.FORENAME + ' ' + e.SURNAME as Name
from EMPLOYEE_TABLE e

where e.ID = @ID
and e.DATE_LEFT IS NULL or e.DATE_LEFT > GETDATE()

UNION ALL

select DISTINCT e.EMPLOY_REF, e.FORENAME + ' ' + e.SURNAME as Name
from EMPLOYEE_TABLE e
where e.DATE_LEFT IS NULL or e.DATE_LEFT > GETDATE()
ORDER BY Name    

You can add a dummy select in between 您可以在两者之间添加一个虚拟选择

SELECT 'Name 1' AS A, 1 AS sortOrder
UNION ALL
SELECT '----' AS B, 2 AS sortOrder
UNION ALL
SELECT 'Name 2' AS C, 3 AS sortOrder
ORDER BY sortOrder, A

In your case this would look something like this 在你的情况下,看起来像这样

SELECT DISTINCT e.EMPLOY_REF, e.FORENAME + ' ' + e.SURNAME as Name, 1 AS sortOrderFROM EMPLOYEE_TABLE e
UNION ALL
SELECT '----', '----' AS B, 2 AS sortOrder
UNION ALL
SELECT DISTINCT e.EMPLOY_REF, e.FORENAME + ' ' + e.SURNAME as Name, 3 AS sortOrderFROM EMPLOYEE_TABLE e
ORDER BY sortOrder, Name

Your best bet may be to include some sort of Sort Order field in each of your queries. 最好的选择是在每个查询中都包含某种“排序顺序”字段。 Maybe an identifier such as an astrick marking them preferred. 也许是像星号这样的标识符标记了它们的首选。

-- Preferred Employees
SELECT '* ' + NameInfo, 1
FROM Employees
WHERE YaddaYadda

UNION

-- Normal Employees
SELECT NameInfo, 2
FROM Employees
WHERE YaddaYadda

Then you can use a sort by in your code to sort first by the sort order, then by name. 然后,您可以在代码中使用sort by,以先按排序顺序排序,然后再按名称排序。

Can you try following 你可以尝试跟随

SELECT 0, 'Preferred Staff Members' 
UNION ALL
select DISTINCT e.EMPLOY_REF, e.FORENAME + ' ' + e.SURNAME as Name
from EMPLOYEE_TABLE e

where e.ID = @ID
and e.DATE_LEFT IS NULL or e.DATE_LEFT > GETDATE()

UNION ALL

SELECT -1, 'All Other Staff members' 
UNION ALL


select DISTINCT e.EMPLOY_REF, e.FORENAME + ' ' + e.SURNAME as Name
from EMPLOYEE_TABLE e
where e.DATE_LEFT IS NULL or e.DATE_LEFT > GETDATE()
ORDER BY Name

You can then easily identify which option is selected by (0 or -1) 然后,您可以轻松识别由(0或-1)选择的选项

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

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