Dan Rosenstark
Dan Rosenstark

Reputation: 69757

How can I do boolean logic on two columns in MySQL?

I want to do a select in MySql that combines several columns... something like this pseudocode:

SELECT payment1_paid AND payment2_paid AS paid_in_full 
FROM denormalized_payments 
WHERE payment1_type = 'check';

Edit: payment1_paid and payment2_paid are booleans.

I can't use any other language for this particular problem than MySql.

Thanks for any help!

Edit: Sorry to everybody who gave me suggestions for summing and concatenating, but I've voted those early answers up because they're useful anyway. And thanks to everybody for your incredibly quick answers!

Upvotes: 6

Views: 4436

Answers (7)

dshaw
dshaw

Reputation: 2159

select (payment1_paid && payment2_paid) as paid_in_full
from denormalized_payments where payment1_type = 'check';

Upvotes: 0

Robert Gamble
Robert Gamble

Reputation: 109022

If by combine you mean concatenate then this will work:

select concat(payment1_paid, payment2_paid) as paid_in_full
from denormalized_payments where payment1_type = 'check';

If by combine you mean add, then this should work:

select payment1_paid + payment2_paid as paid_in_full
from denormalized_payments where payment1_type = 'check';

[EDIT]

For boolean AND:

select payment1_paid && payment2_paid as paid_in_full
from denormalized_payments where payment1_type = 'check';

Upvotes: 2

Eric Hogue
Eric Hogue

Reputation: 8920

Ok, for logical and you can do

Select (payment1_paid && payment2_paid) as paid_in_full 
from denormalized_payments 
where payment1_type = 'check';

As seen here.

Upvotes: 5

David Santamaria
David Santamaria

Reputation: 8811

If are Strings (or you want to treat like Strings the columns that you want to combine) you can use CONCAT and CONCAT_WS. Good luck!

Upvotes: 0

Greg
Greg

Reputation: 321628

SELECT IF(payment1_paid = 1 AND payment2_paid = 1, 1, 0) AS paid_in_fill

Upvotes: 1

mohammedn
mohammedn

Reputation: 2950

I am not sure but do you mean to concatenate?

SELECT CONCAT(ColumnA, ColumnB) AS ColumnZ
FROM Table

Upvotes: 1

Eric Hogue
Eric Hogue

Reputation: 8920

Just do

Select CONCAT(payment1_paid, payment2_paid) as paid_in_full 
from denormalized_payments 
where payment1_type = 'check';

You can concat any number of field you want.

Upvotes: 2

Related Questions