开发者

Comparing two rows in mysql with additional check

Heysa,

I have this little problem that im not quite good at mysql queries and i don't know proper terminology for this question so i'll just roll as i can :)

I have 2 rows in 1 table that i want to compare if they are not 0 and are equal. In php it would look something like this:

if(!empty($row1) && !empty($row2)){
   if($row1==$row2){
       return true;
   }else{
      return false;
   }
}else{
   return false;
}

Here is table if that's any help

id | b | t |
-----------
1 | 2 | 1 |
2 | 0 | 开发者_StackOverflow社区0 | 
3 | 1 | 1 |

And this:

SELECT * FROM table WHERE b=t

Returns also 0=0 record with id=2 ( that i don't want )

Correct query should return only record with id=3


SELECT * FROM table WHERE b = t AND b <> 0 AND t <> 0

Since we're testing that b = t, we know that if b <> 0 is true, then t <> 0 is also true. We can therefore shorten it to

SELECT * FROM table WHERE b = t AND b <> 0

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜