【发布时间】:2021-08-14 18:51:07
【问题描述】:
我是 mysql 的新手。请帮忙
我想在子查询中使用当前行的帖子 ID 来进行计数。 我应该在查询中使用什么来代替“current_row_post_id”
SELECT post_id, title, description, (SELECT COUNT(*) FROM posts_reacts WHERE react_type_id = 1 AND posts.post_id = ? current_row_post_id ) AS total_love, (SELECT COUNT(*) FROM posts_reacts WHERE react_type_id = 2 AND posts.post_id = ? current_row_post_id) AS total_tanks FROM posts;
帖子表说明:
+--------------+---------------+----+---+-----------------+---------------------------------------------+
|Field |Type |Null|Key|Default |Extra |
+--------------+---------------+----+---+-----------------+---------------------------------------------+
|post_id |bigint unsigned|NO |PRI|NULL |auto_increment |
|post_type |varchar(20) |NO | |NULL | |
|title |varchar(256) |YES | |NULL | |
|description |text |YES | |NULL | |
|group_id |bigint unsigned|YES | |NULL | |
|shared_post_id|bigint unsigned|YES | |NULL | |
|user_id |bigint unsigned|NO | |NULL | |
|created_at |timestamp |NO | |CURRENT_TIMESTAMP|DEFAULT_GENERATED |
|updated_at |timestamp |NO | |CURRENT_TIMESTAMP|DEFAULT_GENERATED on update CURRENT_TIMESTAMP|
+--------------+---------------+----+---+-----------------+---------------------------------------------+
posts_reacts 表说明:
+-------------+---------------+----+---+-----------------+---------------------------------------------+
|Field |Type |Null|Key|Default |Extra |
+-------------+---------------+----+---+-----------------+---------------------------------------------+
|post_react_id|bigint unsigned|NO |PRI|NULL |auto_increment |
|post_id |bigint unsigned|NO |MUL|NULL | |
|user_id |bigint unsigned|NO | |NULL | |
|react_type_id|int |NO | |NULL | |
|created_at |timestamp |NO | |CURRENT_TIMESTAMP|DEFAULT_GENERATED |
|updated_at |timestamp |NO | |CURRENT_TIMESTAMP|DEFAULT_GENERATED on update CURRENT_TIMESTAMP|
+-------------+---------------+----+---+-----------------+---------------------------------------------+
【问题讨论】:
标签: mysql-8.0