我有一个返回连接表的查询,如下所示:
| id_cart | id_customer | date_add |
| ------- | ----------- | --------------------- |
| 1 | 100 | 2017-07-24 | 10:48:00 |
| ------- | ----------- | --------------------- |
| 2 | 101 | 2016-02-14 | 15:43:05 |
| ------- | ----------- | --------------------- |
| 3 | 100 | 2015-04-12 | 01:59:34 |
我想这样做,以便从这些结果中只选择id_customer
列中出现多个行的行 - 我假设它看起来像这样:
| id_cart | id_customer | date_add | customer_count |
| ------- | ----------- | --------------------- | -------------- |
| 1 | 100 | 2017-07-24 | 10:48:00 | 2 |
| ------- | ----------- | --------------------- | -------------- |
| 3 | 100 | 2015-04-12 | 01:59:34 | 2 |
我面临的问题是我从其他人那里看到的大多数问题都在寻找多个值出现的计数,而我只对结果感兴趣 - 而不是计数。另外,通过使用连接表,我不确定如何将其作为变量/别名引用回来,而不仅仅是复制&一遍又一遍地粘贴一个可怕的长期查询。
答案 0 :(得分:1)
对于MySQL,我会这样做:
select *
from (select stuff from bigjoin) a1
inner join
(
select id_customer
from (select stuff from bigjoin)
group by id_customer
having count(*) > 1
) x1
on x1.id_Customer = a1.id_customer