的問題是表違反了第一範式,EmpLotusNotes不應該包含僱員和國家,想必他們在工作的國家的名字。
你應該質疑你不允許清理結構和數據的原因。
見https://www.google.com.au/search?q=sql+first+normal+form+atomic
答案,如果你仍然不能後有挑戰性的標準化數據庫,爲創建國家查詢,創建一個查詢到在第一個表中的數據拆分爲第一範式,然後再加入兩。
一個適用於mysql的示例如下,對於MS SQL,您將使用CHARINDEX而不是INSTR和substring而不是substr。
select employeesWithCountries.*
, countries.sort
from (
select empId, empLotusNotes, substr(empLotusNotes, afterStartOfDelimiter) country from (
select empId
, empLotusNotes
, INSTR(empLotusNotes, '/') + 1 as afterStartOfDelimiter
from EmployeesLotusNotes
) employees
) employeesWithCountries
inner join (
SELECT 'Japan' as country, 1 as sort
union
SELECT 'China' as country, 2 as sort
union
SELECT 'India' as country, 3 as sort
union
SELECT 'USA' as country, 4 as sort
) countries
on employeesWithCountries.country = countries.country
order by countries.sort, employeesWithCountries.empLotusNotes
結果。
30003 Kyo Jun/Japan Japan 1
40004 Jee Lee/China China 2
10001 Amit B/India India 3
20002 Bharat C/India India 3
50005 Xavier K/USA USA 4
SQL Server 2008 R2 R2 – psam
請發佈您的SQL SELECT子句,我們不能幫助您。如果你可以創建一個[SQLFiddle](http://sqlfiddle.com/),那真是太棒了...... – ppeterka
這個國家是在一個單獨的列還是EmpLotusNotes列的一部分嗎? – Jacco