如何在MySQL中的两列上进行布尔逻辑?

时间:2008-10-29 22:38:43

标签: sql mysql select boolean

我想在MySql中做一个结合了几个列的select ...类似于这个伪代码:

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

修改:payment1_paid和payment2_paid是布尔值。

我不能使用任何其他语言来解决这个问题而不是MySql。

感谢您的帮助!

编辑:对于向我提供求和和连接建议的所有人都很抱歉,但我已经将这些早期答案投票了,因为它们无论如何都很有用。 感谢给大家提供非常快速的答案!

7 个答案:

答案 0 :(得分:4)

好的,对于逻辑而言,你可以做到

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

here

答案 1 :(得分:2)

只做

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

您可以连接任意数量的字段。

答案 2 :(得分:2)

如果通过组合意味着连接,那么这将起作用:

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

如果通过组合你意味着添加,那么这应该工作:

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

[编辑]

对于布尔AND:

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

答案 3 :(得分:1)

我不确定,但你的意思是连接吗?

SELECT CONCAT(ColumnA, ColumnB) AS ColumnZ
FROM Table

答案 4 :(得分:0)

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

答案 5 :(得分:0)

如果是字符串(或者您希望将字符串视为要组合的列),则可以使用CONCATCONCAT_WS。祝你好运!

答案 6 :(得分:0)

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