【问题标题】:how to set every rows primary key for sub Query如何为子查询设置每行主键
【发布时间】: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


    【解决方案1】:

    您可以使用表名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 子句中使用新的列别名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 2014-03-29
      • 2020-06-02
      • 2010-10-31
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多