views:

509

answers:

8

Hi all.

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!

+2  A: 

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.

Eric Hogue
Sorry I didn't specify, they are booleans. Thanks!
Yar
A: 

Do you mean you want them summed or concatenated?

+1  A: 

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';
Robert Gamble
thanks, sorry the question was about booleans but that wasn't clear until later. great answer!
Yar
+1  A: 

I am not sure but do you mean to concatenate?

SELECT CONCAT(ColumnA, ColumnB) AS ColumnZ
FROM Table
mnour
Sorry I didn't specify, they are booleans. Thanks!
Yar
A: 

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

Greg
A: 

Hi, 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!

David Santamaria
+2  A: 

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.

Eric Hogue
Excellent. That's what I needed, with the MySql reference to boot! Thank you.
Yar
A: 
select (payment1_paid && payment2_paid) as paid_in_full
from denormalized_payments where payment1_type = 'check';
dshaw
dshaw