6

这是我的桌子:

Anganbadi_ID               Food     Month
-------------------------------------------    
1165                       हाँ         1
1165                       हाँ         2
1165                       हाँ         4
1168                       हाँ         4
2032                       नहीं        4
2218                       नहीं        4
2219                       हाँ         4
2358                       नहीं        4
2546                        हाँ        10 

有 4 列Anganbadi_ID, Food, Month, Year,我想Food根据两个不同的月份值比较列两次。

例如,如果我为第一个 Food (Food-1) 列选择 Month=4,为第二个 Food (Food-2) 列选择 Month=10,那么它应该如下所示:

Anganbadi_ID            Food-1     Food-2    
------------------------------------------    
1165                       हाँ          NULL 
1168                       हाँ          NULL 
2032                       नहीं        NULL 
2218                       नहीं        NULL 
2219                       हाँ          NULL 
2358                       नहीं        NULL 
2546                     NULL        हाँ 

当我尝试这段代码时

SELECT     
   Anganbadi_ID, Food,
   (SELECT Food
    FROM Anganbadi AS Anganbadi_2
    WHERE (Anganbadi_1.Anganbadi_ID = Anganbadi_ID) 
      AND (Anganbadi_1.Month = 10)
   ) AS 'Food(2)'
FROM Anganbadi AS Anganbadi_1
WHERE (Month = 4)

它显示以下结果:

Anganbadi_ID              Food-1     Food-2    
--------------------------------------------
1165                       हाँ          NULL 
1168                       हाँ          NULL 
2032                       नहीं        NULL 
2218                       नहीं        NULL 
2219                       हाँ          NULL 
2358                       नहीं        NULL 

请尽快帮助我......

4

5 回答 5

12
SELECT DISTINCT Anganbadi_ID,

(SELECT Food 
 FROM Anganbadi 
 WHERE      (Anganbadi_ID = A.Anganbadi_ID) 
    AND (Month = 4)) AS Food1,

(SELECT Food 
 FROM Anganbadi 
 WHERE      (Anganbadi_ID = A.Anganbadi_ID) 
    AND (Month = 10)) AS Food2

FROM Anganbadi AS A 
WHERE A.Month = 10 OR A.Month = 4

SQL小提琴

于 2013-05-07T16:37:18.190 回答
1
SELECT Anganbadi_ID, Food as food1, null as Food2
where Month = 4

UNION ALL

SELECT Anganbadi_ID, null as food1, food as Food2
where Month = 10
于 2013-05-07T16:37:15.897 回答
1

这里的关键是 ID 上的完全外连接,每个子查询中的条件可以是任何东西,完全外连接会给你一行 ID 匹配的行,而相反的列则为空。如果您想要所有结果,即使食物在任一月份都不满足任何条件,则在其中一个子查询中使用 AllFoodsTable 中的选择 ID 进行左连接

SELECT Food1, Food2, ID
FROM (
  SELECT Food1, ID
  FROM MyTable
  WHERE Month = 4
) Con1
FULL OUTER JOIN (
  SELECT Food2, ID
  FROM MyTable
  WHERE Month = 10
) Con2
ON Con1.ID = Con2.ID
于 2013-05-07T16:33:28.837 回答
0
SELECT DISTINCT Anganbadi_ID
,(SELECT     Food
FROM          Anganbadi AS Anganbadi_2
WHERE      (Anganbadi_1.Anganbadi_ID = Anganbadi_ID) AND (Month = 4)) AS 'Food(4)'
,(SELECT     Food
FROM          Anganbadi AS Anganbadi_2
WHERE      (Anganbadi_1.Anganbadi_ID = Anganbadi_ID) AND (Month = 10)) AS 'Food(10)'

FROM         Anganbadi AS Anganbadi_1

SQL-Fiddle

于 2013-05-07T16:35:58.543 回答
0
SELECT 
Food
DISTINCT Anganbadi_ID,

(SELECT Food 
 FROM Anganbadi 
 WHERE      (Anganbadi_ID = A.Anganbadi_ID) 
    AND (Month = 4)) AS Food1,

FROM Anganbadi AS A 
WHERE A.Month = 10 OR A.Month = 4
于 2018-04-06T11:32:04.220 回答