我有一个包含多个具有布尔值的字段的表。
只有当所有3个字段都为真时,才需要合并3个字段。
我在考虑JOIN
,但看起来它无法满足我的需求。
------------------------------------------------------------
| Email address| permission1 | permission2 | permission3
------------------------------------------------------------
| test@test.com| 1 | 0 | 1
------------------------------------------------------------
| test2@test.com| 1 | 1 | 1
------------------------------------------------------------
我希望结果可能在新表中看起来像这样:
--------------------------------
| Email address| permissionMerge
--------------------------------
| test@test.com| 0 |
--------------------------------
| test2@test.com| 1 |
--------------------------------
答案 0 :(得分:2)
您可以使用CASE syntax
SELECT `email address`,
CASE WHEN `permission1` = 1
AND `permission2` = 1
AND `permission3` = 1
THEN 1
ELSE 0 END AS 'permissionMerge'
FROM `Table`
您也可以这样做:
SELECT `email address`,
CASE WHEN SUM(`permission1`,`permission2`,`permission3`) = 3
THEN 1
ELSE 0 END AS 'permissionMerge'
FROM `Table`
GROUP BY `email address`
答案 1 :(得分:1)
Select `Email adderss`,(permission1 and permission2 and permission3) as permissionMerge from table
答案 2 :(得分:1)
根据数据的质量,您可以轻而易举地完成以下任务:
SELECT address,
CASE SUM(permission1, permission2, permission3)
WHEN 3 THEN 1
ELSE 0
END AS permissionMerge
FROM undisclosed_table
......或者你需要更强大的东西:
SELECT address,
CASE
WHEN permission1 IS TRUE AND permission2 IS TRUE AND permission3 IS TRUE THEN 1
ELSE 0
END AS permissionMerge
FROM undisclosed_table
您可以使用以下内容生成新表:
CREATE TABLE table_with_duplicate_data
SELECT address,
CASE
WHEN permission1 IS TRUE AND permission2 IS TRUE AND permission3 IS TRUE THEN 1
ELSE 0
END AS permissionMerge
FROM undisclosed_table
...以为我会反对它:它只会浪费存储空间,你需要保持数据的最新状态。如果输入太多,您可以创建一个view或(自MySQL / 5.7.5开始)generated column。