简体   繁体   English

根据表1上的每个ID连接2个表

[英]Joining 2 Table based from each id on table 1

Let's say.. I have 2 table like this 比方说..我有2张桌子

Table 1 表格1

表格1

Table 2 表2

TABLE_2

I want to join 2 table above. 我想加入上面的2表。 Display each content from column name_tb1 on Table 1 and get the output of total/num rows from name_tb2 on Table 2 based from each of id_tb1 显示表1name_tb1列中的每个内容,并根据id_tb1的每个获取 表2中 name_tb2的总行/行的输出

Example Output: 示例输出:

First (3 Row)
Second (2 Row)
Third (3 Row)

This should do the trick. 这应该可以解决问题。

SELECT
    table_1.id_tb1, 
    count(*) 
FROM
    table_1, 
    table_2 
WHERE 
    table_1.id_tb1 = table_2.id_tb1 
GROUP BY 
    table_1.id_tb1

ON a sidenote, not thrilled by your naming convention and image. 在旁注中,不要为您的命名约定和图像感到兴奋。 We like to help but copy and paste makes all easier. 我们希望提供帮助,但复制和粘贴使一切变得简单。 From a image we can't copy/paste text. 从图像中,我们无法复制/粘贴文本。

For naming call the PRI-KEY of a table 'id', and prefix the foreign key with the table name. 对于命名,请调用表“ id”的PRI-KEY,并在外键之前添加表名。

SO: 所以:

  • table1.id table1.id
  • table1.name table1.name
  • table2.id table2.id
  • table2.table1_id table2.table1_id
  • table2.name table2.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM