tags:

views:

121

answers:

5

I have two tables in my database (let's say t1 and t2) and I have a field of type BIT in each table (let's call it field1), what I want to do is have the same functionality obtained by using the && boolean operator (like in C, C#, Java .. etc), or in other words I want to do this:

select t1.field1 && t2.field1 from t1, t2 where .. (if field1 is false in one of the tables, it should return false, to return true it has to be true in both tables)

I know it can be done with a use case (or IF() in MySQL), but I'm curious to know if there are other ways ...

+4  A: 
SELECT t1.field & t2.field
Quassnoi
Brian R. Bondy
Quassnoi
Ya but if 0 or non zero is seen as false or true, then I could see how this would eventually lead to a bug. But I agree if you are for sure only using a BIT field it will be ok.
Brian R. Bondy
+1  A: 
SELECT t1.field1 && t2.field1 FROM t1, t2 WHERE t1.field1 = 1 AND t2.field2 = 1
awithrow
A: 

There isn't a standard SQL way to do it - any such mechanism is DBMS-specific.

Jonathan Leffler
post is tagged mysql, so question is DMBS specific
Patrick McDonald
A: 

(using T-SQL - might be same syntax for mysql?)

If you include the & in the WHERE clause that will evaluate as AND

SELECT    t1.field1, t2.field1
FROM         t1, t2
WHERE t1.field1 & t2.field1 = 1

kevchadders
+1  A: 

AND and && are equivalent. So you can either use “t1.field1 && t2.field1” or “t1.field1 AND t2.field1”:

SELECT t1.field1 && t2.field1
FROM …

SELECT t1.field1 AND t2.field1
FROM …
Gumbo