6

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!

0

7 Answers 7

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. That's what I needed, with the MySql reference to boot! Thank you.
2

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.

Comments

2

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';

1 Comment

thanks, sorry the question was about booleans but that wasn't clear until later. great answer!
1

I am not sure but do you mean to concatenate?

SELECT CONCAT(ColumnA, ColumnB) AS ColumnZ
FROM Table

Comments

1

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

Comments

0

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!

Comments

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

1 Comment

Actually, It you should go with the Logical AND (&&) as @Robert Gamble mentioned since it should allow short circuit evaluation. Thus if payment1_paid is false, it's false. dev.mysql.com/doc/refman/5.0/en/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.