如何为子查询设置每一行主键

how to set every rows primary key for sub Query

我是 mysql 的新人。请帮忙

我想使用当前行 post 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;

posts table 描述:

+--------------+---------------+----+---+-----------------+---------------------------------------------+
|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 table 描述:

+-------------+---------------+----+---+-----------------+---------------------------------------------+
|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|
+-------------+---------------+----+---+-----------------+---------------------------------------------+


您可以使用 table 名称 posts_reacts

AND posts.post_id = posts_reacts.post_id

或子查询别名 total_lovetotal_tanks

AND posts.post_id = total_love.post_id
AND posts.post_id = total_tanks.post_id

限定不明确的列post_id。确保删除 current_row_post_id,因为在 where 子句中不能有新的列别名。