本文介绍了sql server:选择所述语言的颜色作为项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好! 我几乎是新手用sql ..但是尝试建立一个查询..我的知识太复杂了: 问题是处理不同语言的颜色 我有 COLORS table 颜色 以下字段 ID EN DE FR PT 1 white weiss blanc branco 2 black schwarz noir preto 3 red rot rouge vermelho ..... 然后我有ITEMS表,其中包括其他产品,有一个颜色字段..一些石灰这个 ID ItemID模型颜色 1 MT002 S9500黑色#white#red 2 MZ412 W8 black#red 3 MZ415 W8s black#white 4 MZ499 N9500红色 给出以下参数 语言=FR SelectedProduct ='MZ412','MZ415' 带有查询,我想选择所选产品的型号选定语言的相对颜色;即:我期待这样的输出: 型号CLRS W8 blanc#rouge W8s noir #blanc 我这样做了: SELECT Items.Model,( SELECT fr FROM 颜色其中 colors.EN in ( case 何时 charindex(' #',items.Colors)= 0 然后 items.colors else left (items.Colors,charindex(' #',items.Colors)-1) end )) + ' #' +( SELECT fr FROM 颜色 where colors.EN in ( case when charindex(' #',items.Colors)= 0 items.colors else right (items.Colors,charindex(' #', reverse(items.Colors)) - 1) end )) as CLRS FROM 项 WHERE Items.ID IN (' MZ412',' MZ415') 有效..但仅限于1或2种颜色。 。 如果没有颜色可以修复..但是如果有3种或更多颜色呢? 可以给出路径的概念关注? 谢谢 Jan 解决方案 世界充满了不同的奇妙方法;) 我不喜欢使用 CASE WHEN ... END [ ^ ]声明。我的目的是使用公用表格表达式 [ ^ ](CTE)。这不完全是你想要的,但它显示了解决问题的方法。 - 颜色表变量 DECLARE @ colors TABLE (ID INT IDENTITY ( 1 , 1 ),EN VARCHAR ( 30 ),DE VARCHAR ( 30 ),FR VARCHAR ( 30 ),PT VARCHAR ( 30 )) INSERT INTO @ colors (EN,DE,FR,PT) SELECT ' white',' weiss',' blanc',' branco' UNION ALL SELECT ' black',' schwarz' ,' noir',' preto' UNION ALL SELECT ' red',' 腐烂, rouge',' vermelho' - items table variable DECLARE @ items 表(ID INT IDENTITY ( 1 , 1 ),ItemID VARCHAR ( 30 ),Model VARCHAR ( 30 ),颜色 VARCHAR ( 100 )) INSERT INTO @ items (ItemID,型号,颜色) SELECT ' MT002',' S9500',' 黑色#white#red' UNION ALL SELECT ' MZ412', ' W8',' black#red ' UNION ALL SELECT ' MZ415',' W8s',' black#white' UNION ALL SELECT ' MZ499',' N9500',' red' - 输入参数: DECLARE @ lng VARCHAR ( 30 ) SET @ lng = ' FR' DECLARE @ products VARCHAR ( 30 ) SET @ products = ' MT002,MZ412,MZ415' - 首次CTE - 将ItemID拆分为单独的行 ; WITH SelectedProducts AS ( - 初始值 SELECT LEFT ( @ products ,CHARINDEX(' ,', @ products ) - 1) AS ItemID, RIGHT ( @ products ,LEN( @ products ) - CHARINDEX(' ,', @ products )) AS 剩余 WHERE CHARINDEX(' ,', @ products )> 0 UNION ALL - 递归部分 SELECT LEFT (Remainder, CHARINDEX(',', Remainder)-1) AS ItemID, RIGHT(Remainder, LEN(Remainder)-CHARINDEX(',', Remainder)) AS Remainder \tFROM SelectedProducts \tWHERE CHARINDEX(',', Remainder)>0 \tUNION ALL \tSELECT Remainder AS ItemID, NULL AS Remainder \tFROM SelectedProducts \tWHERE CHARINDEX(',', Remainder)=0 ), CurrentColors AS --second CTE, split colors \t( \t\t--initial value \t\tSELECT sp.ItemID, i.Model, LEFT(i.Colors, CHARINDEX('#',i.Colors)-1) AS Color, RIGHT(i.Colors, LEN(i.Colors)-CHARINDEX('#',i.Colors)) AS CRemainder \t\tFROM SelectedProducts AS sp INNER JOIN @items AS i ON i.ItemID = sp.ItemID \t\tWHERE CHARINDEX('#',i.Colors)>0 \t\tUNION ALL \t\t--recursive part \t\tSELECT ItemID, Model, LEFT(CRemainder, CHARINDEX('#',CRemainder)-1) AS Color, RIGHT(CRemainder, LEN(CRemainder)-CHARINDEX('#',CRemainder)) AS CRemainder \t\tFROM CurrentColors \t\tWHERE CHARINDEX('#',CRemainder)>0 \t\tUNION ALL \t\tSELECT ItemID, Model, CRemainder AS Color, NULL AS CRemainder \t\tFROM CurrentColors \t\tWHERE CHARINDEX('#',CRemainder)=0 \t) --final result set \t\tSELECT cc.ItemID, cc.Model, cc.Color AS EN, c.DE, c.FR, c.PT \t\tFROM CurrentColors AS cc INNER JOIN @colors AS c ON cc.Color=c.EN \t\tORDER BY cc.ItemID Result:ItemID Model EN DE FR PT MT002\tS9500\tblack\tschwarz\tnoir\tpreto MT002\tS9500\twhite\tweiss\tblanc\tbranco MT002\tS9500\tred\trot\trouge\tvermelho MZ412\tW8\tred\trot\trouge\tvermelho MZ412\tW8\tblack\tschwarz\tnoir\tpre to MZ415\tW8s\tblack\tschwarz\tnoir\tpreto MZ415\tW8s\twhite\tweiss\tblanc\tbranco By The Way: i would suggest you to change design of Items table. Instead using english names of colors, use them ID. Also, colors column should store values: 1#2#3 2#3 ...etc Why? It is easiest to fetch color by its ID, than its name (in English). If you would like to fetch color Id’s in any language, you can use UNPIVOT[^] table to achieve that: SELECT ID, ColorName, Lngg FROM ( SELECT * FROM @colors ) AS pvt UNPIVOT(ColorName FOR Lngg IN([EN],[DE],[FR],[PT])) AS unpvt WHERE Lngg = @lng Result (in case of ’FR’ as an input parameter): ID ColorName\tLngg 1\tblanc\t\tFR 2\tnoir\t\tFR 3\trouge\t\tFRHi everybody!I'm almost newbie with sql.. but trying build a query.. too complicated for my knowledge:Issue is concering handling colors in different languagesI have "COLORS" table with colors with following fieldsIDENDEFRPT1whiteweissblancbranco2blackschwarznoirpreto3redrotrougevermelho.....then I have "ITEMS" table with products that, among others, has a "colors" field.. something lime thisIDItemIDModelColors1MT002S9500black#white#red2 MZ412W8black#red3MZ415W8sblack#white4 MZ499 N9500 redgiven the following parametersLanguage="FR"SelectedProduct="'MZ412','MZ415'"with a query , I want select the models of the selected product with relative colors in selected language; ie: I expect something like this as output:Model CLRSW8 blanc#rougeW8snoir#blancI did this:SELECT Items.Model, (SELECT fr FROM Colors where colors.EN in (case when charindex('#',items.Colors) = 0 then items.colors else left(items.Colors,charindex('#',items.Colors)-1)end )) +'#'+(SELECT fr FROM Colors where colors.EN in (case when charindex('#',items.Colors) = 0 then items.colors else right(items.Colors,charindex('#',reverse(items.Colors))-1)end )) as CLRS FROM Items WHERE Items.ID IN ('MZ412','MZ415')that works.. but only if have 1 or 2 colors.. can fix if have no color.. but what about if have 3 or more colors?can give an idea of the path to follow?ThanksJan 解决方案 这篇关于sql server:选择所述语言的颜色作为项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 22:14