这是我的表:
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列两次.
例如如果我选择Month = 4作为第一个Food(Food-1)列,Month = 10作为第二个Food(Food-2)列,那么它应该如下::
Anganbadi_ID Food-1 Food-2 ------------------------------------------ 1165 हाँ NULL 1168 हाँ NULL 2032 नहीं NULL 2218 नहीं NULL 2219 हाँ NULL 2358 नहीं NULL 2546 NULL हाँ
当我尝试这段代码时
SELECT Anganbadi_ID,(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
请尽快帮助我….
解决方法
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