这是我所拥有的粗略想法:
+---------------+ +-----------------+ +---------------+
| customers | | transactions | | branches |
+---------------+ +-----------------+ +---------------+
| customerid | | orderid | | branchid |
| | | customerid (FK) | | |
| | | branchid (FK) | | |
+---------------+ | datetime | +---------------+
+-----------------+
我将如何创建一个查询来获取每天/每周/等的交易计数。但是对于每个 branchid分别?
我试过这样的工会(只计算所有星期六)。这给出了一个结果,但不是所需的格式。
SELECT COUNT(orderid) as count1 FROM transactions WHERE WEEKDAY(datetime) = 5
AND branchid = 'branch1'
UNION
SELECT COUNT(orderid) as count2 FROM transactions WHERE WEEKDAY(datetime) = 5
AND branchid = 'branch2'
返回:
+------------+
| count1 |
+------------+
| 152 |
| 48 |
+------------+
而我希望数据格式如下:
+------------+------------+
| count1 | count2 |
+------------+------------+
| 152 | 48 |
| | |
+------------+------------+
有人对如何做到这一点有任何提示吗?提前致谢!