我怎么能在MySQL在两列布尔逻辑吗?How can I do boolean logic on two columns in MySQL?

- 此内容更新于:2014-12-30
主题:

原文:

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!

解决方案:
好,逻辑,你能做的 在这里可以看到。
原文:

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.

Yar的回复:太好了。这# 39;我需要,引导与MySql引用!谢谢你!

(原文:Excellent. That's what I needed, with the MySql reference to boot! Thank you.)

解决方案:
只做 你想要可以concat任意数量的字段。
原文:

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.

Yar的回复:对不起,我没有# 39;t指定,他们是布尔值。谢谢!

(原文:Sorry I didn't specify, they are booleans. Thanks!)

解决方案:
如果把你的意思连接,那么这将工作: 如果把你的意思添加,那么这应该工作: 选择payment1_paid + payment2_paid paid_in_full 从denormalized_payments payment1_type =“检查”; (编辑) 布尔和: 选择payment1_paid,及payment2_paid paid_in_full 从denormalized_payments payment1_type =“检查”;
原文:

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';
Yar的回复:谢谢,对不起这个问题是关于布尔运算,并# 39;t清楚,直到后来。很棒的回答!

(原文:thanks, sorry the question was about booleans but that wasn't clear until later. great answer!)

解决方案:
我不确定但你是连接吗?
原文:

I am not sure but do you mean to concatenate?

SELECT CONCAT(ColumnA, ColumnB) AS ColumnZ
FROM Table
Yar的回复:对不起,我没有# 39;t指定,他们是布尔值。谢谢!

(原文:Sorry I didn't specify, they are booleans. Thanks!)

解决方案:
选择如果(payment1_paid = 1和payment2_paid = 1,1,0)paid_in_fill
原文:

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

解决方案:
如果是字符串(或你想治疗像字符串列要结合)可以使用CONCAT和CONCAT_WS。好运!
原文:

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!

解决方案:
原文:
select (payment1_paid && payment2_paid) as paid_in_full
from denormalized_payments where payment1_type = 'check';
dshaw的回复:实际上,它与逻辑,你应该去(,,)如@Robert赌博述因为它应该允许短路评估。因此如果payment1_paid是假的,这# 39;年代错误的。dev.mysql.com/doc/refman/5.0/en/…

(原文: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/…)