开发者

help for sql query

开发者 https://www.devze.com 2023-01-06 01:51 出处:网络
i have table with name sample in my database it has threecolumns namely words,D1,D2 and it has some data like below

i have table with name sample in my database it has threecolumns namely words,D1,D2 and it has some data like below

Words      D1     D2  D3
pleasure    1      0   1
question    0      0   0         
answer      0      1   1
request     1      0   0
scount      1      0   0

so now i want to calculate parameter N00 which 开发者_JAVA技巧means scount=0 and also it should check where 0 exists in D1 and D2 so here for "question" D1=0 D2=0 and scount=0 so the result should be 2 because scount=0 and D1=0 , scount=0 and D2=0 this both satisfy so result is 2 i need sql query fro this please help advance thanks.


If I'm reading correctly you want to know how many records there are with scount = 0 and either D1 or D2 = 0, that's a query like this:

SELECT COUNT(*) as N00 FROM mytable WHERE scount = 0 AND (D1 = 0 OR D2 = 0)

on second pass I think you're trying to count conditions satisfied, that is, +1 when D1 = 0 and +1 when D2 = 0, but only where scount = 0, that's something like this:

SELECT (IF(D1=0,1,0)+IF(D2=0,1,0)) as N00 WHERE scount = 0


Your question doesn't add up. You act as if scount is a column but it is a row. And tbh your question is not making sense. Could you perhaps split this up in separate cases like:

A: (D1 = 0) AND (D2 = 0)  -> N00 = X 
B: (D1 = 0) AND (D2 = 1)  -> N00 = Y 

I don't have any idea how to fit in scount in these cases. You cant say the following:

C: (D1 = 0) AND (D2 = 0) AND (scount = 1) -> N00 = Z

This isn't possible because scount is in a different row and has no relation to this row at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消