选择总时间计数外键存在于表中
我的表设置如下如下候选
名单
ID(primary键) | property_id(外键) | user_id |
---|---|---|
1 | 100 | 2 |
2 | 101 | 2 |
3 | 103 | 4 |
4 4 | 100 | 1 |
5 | 100 | 3 |
6 | 101 | 1 |
属性
ID(主键) | account_id |
---|---|
100 | 1 |
101 | 2 |
102 3 103 | 103 |
103 | 4 |
然后,我有以下查询,
SELECT DISTINCT `s`.`user_id`, `s`.`property_id`, `p`.`id`, `p`.`account_number`, COUNT(`s`.`property_id`) as `shortlistCount`
FROM `shortlist` `s`
INNER JOIN `property` `p` ON s.`property_id` = p.`id`
WHERE s.`user_id` = "2"
GROUP BY s.`property_id`
LIMIT 10
我想添加一个计数字段别名作为shortlistCount
,该返回每个property_id
字段中出现在shortlist 表。 (或
ID
属性
表中的字段,以此上下文中更容易的人),
例如property_id
100
出现3
times在入围表中。
因此,结果应该是
| user_id | property_id | id | account_number | shortlistCount |
|---------|-------------|-----|----------------|----------------|
| 2 | 100 | 100 | 1 | 3 |
| 2 | 101 | 101 | 2 | 2 |
ShortListCount
当前始终返回1
我如何才能更新我的shortlistCount
别名以返回上述结果?
My table setup is as follows
Shortlist
id (primary key) | property_id(foreign key) | user_id |
---|---|---|
1 | 100 | 2 |
2 | 101 | 2 |
3 | 103 | 4 |
4 | 100 | 1 |
5 | 100 | 3 |
6 | 101 | 1 |
Property
id (primary key) | account_id |
---|---|
100 | 1 |
101 | 2 |
102 | 3 |
103 | 4 |
I then have the following query
SELECT DISTINCT `s`.`user_id`, `s`.`property_id`, `p`.`id`, `p`.`account_number`, COUNT(`s`.`property_id`) as `shortlistCount`
FROM `shortlist` `s`
INNER JOIN `property` `p` ON s.`property_id` = p.`id`
WHERE s.`user_id` = "2"
GROUP BY s.`property_id`
LIMIT 10
I want to add a count field alias as shortlistCount
that returns the total number of times each property_id
field appears in the shortlist
table. (or id
field in the property
table, whichever is easier in this context)
For example, the property_id
of 100
appears 3
times in the shortlist table.
So the result should be
| user_id | property_id | id | account_number | shortlistCount |
|---------|-------------|-----|----------------|----------------|
| 2 | 100 | 100 | 1 | 3 |
| 2 | 101 | 101 | 2 | 2 |
However, shortlistCount
currently always returns 1
How can I can update my shortlistCount
alias to return the above result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要计算
property_id
s,您可以使用相关的子查询:To count the
property_id
s you can use a correlated subquery: