English 中文(简体)
• 如何在1行中 values取价值,而不是在KLQ中 multiple取多行。
原标题:How to fetch the values in 1 row instead of multiple rows in SQL

我的表格显示了所有项目名称及其物品代码。

<>><>>>>>

ItemID, ItemName
772729918,  ABC 
772729921,  BCD 
772729922,  EFG 
772729923,  HIJ 
772729926,  KLM 

我有另一个表格,对项目图像路径作了界定。

itemimageID, Imagesurface,ImagePath, fitemID
111, FRONT ,  772729918_1_1_FRONT.tif , 772729918
112, BACK ,  772729918_1_1_BACK.tif ,772729918
222, FRONT , 772729921_1_1_FRONT .tif,772729921
223, BACK , 772729921_1_1_BACK.tif ,772729921
333, FRONT , 772729922_1_1_FRONT.tif ,772729922
332, BACK ,  772729922_1_1_BACK.tif ,772729922
444, FRONT , 772729923_1_1_FRONT.tif ,772729923
442, BACK ,  772729923_1_1_BACK.tif ,772729923
555, FRONT , 772729926_1_1_FRONT.tif ,772729926
552, BACK ,  772729926_1_1_BACK.tif ,772729926

如果我加入这两个表格的话,我就把价值计算如下。

Select ItemID,ItemName, Imagesurface,ImagePath from TableItem i inner join TableImages ti on ti.fitemID = i.ItemID

772729918, ABC , FRONT , 772729918_1_1_FRONT.tif 
772729918, ABC , BACK , 772729918_1_1_BACK.tif 
772729921, BCD , FRONT , 772729921_1_1_FRONT.tif 
772729921, BCD , BACK , 772729921_1_1_BACK.tif 
772729922, EFG , FRONT , 772729922_1_1_FRONT.tif 
772729922, EFG , BACK , 772729922_1_1_BACK.tif 
772729923, HIJ , FRONT , 772729923_1_1_FRONT.tif 
772729923, HIJ , BACK , 772729923_1_1_BACK.tif 
772729926, KLM , FRONT , 772729926_1_1_FRONT.tif 
772729926, KLM , BACK , 772729926_1_1_BACK.tif 

Actually I want to show them in one row instead of 2 line for each item. Thanks for helping me.

ItemID, ItemName, ImageSurface, ImageFront, ImageBack
772729918, ABC , FRONT , 772729918_1_1_FRONT.tif , 772729918_1_1_BACK.tif 
772729921, BCD , FRONT , 772729921_1_1_FRONT.tif , 772729921_1_1_BACK.tif 
772729922, EFG , FRONT , 772729922_1_1_FRONT.tif , 772729922_1_1_BACK.tif 
772729923, HIJ , FRONT , 772729923_1_1_FRONT.tif , 772729923_1_1_BACK.tif 
772729926, KLM , FRONT , 772729926_1_1_FRONT.tif , 772729926_1_1_BACK.tif 
最佳回答
SELECT itemid, 
       itemname, 
       imagesurface, 
       fti.imagepath AS imagefront, 
       bti.imagepath AS imageback 
FROM   tableitem i 
       LEFT JOIN tableimages fti 
         ON fti.fitemid = i.itemid 
            AND fti.imagesurface =  FRONT  
       LEFT JOIN tableimages bti 
         ON bti.fitemid = i.itemid 
            AND fti.imagesurface =  BACK  
问题回答

如何使用<代码> INNER JOIN work......因为你拥有与你一起生活的条件相匹配的多个浏览器,你也将获得你的成果。

你们可以获得你们希望使用分局的东西。

SELECT   ItemId
        ,ItemName
        --,(SELECT TOP 1 ImageSurface FROM TableImages WHERE fItemId = ItemId)
        ,(SELECT TOP 1 ImagePath FROM TableImages WHERE fItemId = ItemId AND ImageSurface =  FRONT ) ImageFront
        ,(SELECT TOP 1 ImagePath FROM TableImages WHERE fItemId = ItemId AND ImageSurface =  BACK ) ImageBack
FROM    TableItem

请注意,我评论了图像Surface领域,因为它似乎没有必要。

不要让一个人加入

Select i.ItemID, i.ItemName, ti.Imagesurface, ti.ImagePath 
from TableItem i Left join TableImages ti     
on ti.fitemID = i.ItemID

这将包括你左手桌上的所有物品,如果存在,则与右手桌上的所有物品相匹配。 如果没有

你们需要做所谓的“pivot”。 例如:

SELECT pivottable.* FROM
(
  Select ItemID,ItemName, Imagesurface,ImagePath from TableItem i inner join TableImages ti on ti.fitemID = i.ItemID 
) as baseTable
PIVOT
(
min(imagepath) for ImageSurface in ( [FRONT],[BACK] )
) as pivottable




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签