表A
+-------+----------+
| user | product |
+-------+----------+
| user1 | product1 |
| user1 | product2 |
| user1 | product6 |
+-------+----------+
表B
+----------+
| product |
+----------+
| product1 |
| product2 |
| product3 |
| product4 |
| product5 |
| product6 |
+----------+
输出
+-------+----------+----------+----------+----------+----------+----------+
| user | product1 | product2 | product3 | product4 | product5 | product6 |
+-------+----------+----------+----------+----------+----------+----------+
| user1 | 1 | 1 | 0 | 0 | 0 | 1 |
+-------+----------+----------+----------+----------+----------+----------+
Table B
包含Table A
的所有不同产品。它比示例大得多,并且每次运行都是动态的,因此不太可能使用简单的case when
语句。