How can I do boolean logic on two columns in MySQL?

我只是一个虾纸丫 提交于 2020-01-11 08:53:29

问题


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!


回答1:


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.




回答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.




回答3:


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



回答4:


I am not sure but do you mean to concatenate?

SELECT CONCAT(ColumnA, ColumnB) AS ColumnZ
FROM Table



回答5:


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




回答6:


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!




回答7:


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


来源:https://stackoverflow.com/questions/248683/how-can-i-do-boolean-logic-on-two-columns-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!