Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 365
Dan Rosenstark Avatar asked Oct 29 '08 22:10

Dan Rosenstark


4 Answers

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.

like image 118
Eric Hogue Avatar answered Oct 22 '22 12:10

Eric Hogue


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';
like image 28
Robert Gamble Avatar answered Oct 22 '22 11:10

Robert Gamble


I am not sure but do you mean to concatenate?

SELECT CONCAT(ColumnA, ColumnB) AS ColumnZ
FROM Table
like image 1
mohammedn Avatar answered Oct 22 '22 12:10

mohammedn


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

like image 1
Greg Avatar answered Oct 22 '22 13:10

Greg