找到你要的答案

Q:SQL:How to make condition mysql get p.id,count(l.id),count(c.id) from p,l,c?

Q:SQL:如何使情况得到p.id MySQL,计数(l.id),计数(入境)从P,L,C?

table post

------------------
   id   :   name
------------------
    1   :   a
    2   :   b
    3   :   a
------------------

table like

------------------
  like_id : id
------------------
    1     :  1
    2     :  1
    3     :  3
------------------

table comment

----------------------
   comment_id :  id
----------------------
       1      :   1
       2      :   2
       3      :   2

how to get post.id,count(like.id),count(comment.id)

表后

------------------
   id   :   name
------------------
    1   :   a
    2   :   b
    3   :   a
------------------

表像

------------------
  like_id : id
------------------
    1     :  1
    2     :  1
    3     :  3
------------------

评论表

----------------------
   comment_id :  id
----------------------
       1      :   1
       2      :   2
       3      :   2

如何获得邮政,身份证,计数(如身份证),计数(评论)

answer1: 回答1:

First - "like" is a reserved word in SQL, used for regex matching, so you can't use that as a table name. I've renamed this table "post_like" to stop this causing a syntax error.

Then it looks like a job for a correlated subquery:

select post.id, 
  (select count(*) from post_like where post_like.id=post.id) like_count, 
  (select count(*) from comment where comment.id=post.id) comment_count
from
  post;

第一个“喜欢”是一个保留字在SQL中,使用正则表达式匹配,所以你不能用这个表名。我把这个表”post_like”停止这导致语法错误。

然后,它看起来像是一个相关子查询工作:

select post.id, 
  (select count(*) from post_like where post_like.id=post.id) like_count, 
  (select count(*) from comment where comment.id=post.id) comment_count
from
  post;
mysql  sql