主表如何统计在附表中的出现次数?

建立主表:

create table a1 (id int ,name char(20));

建立附表:

create table a2 (tid int ,name char(20));

分别插入数据:

mysql> insert into a1 values (1,‘baidu‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into a1 values (2,‘qq‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into a1 values (3,‘sina‘);
Query OK, 1 row affected (0.02 sec)

mysql> insert into a2 values (1,‘000‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into a2 values (1,‘001‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into a2 values (1,‘002‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into a2 values (1,‘003‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into a2 values (2,‘200‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into a2 values (2,‘201‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into a2 values (2,‘202‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into a2 values (2,‘203‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into a2 values (2,‘204‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into a2 values (2,‘205‘);
Query OK, 1 row affected (0.01 sec)

mysql> insert into a2 values (3,‘300‘);
Query OK, 1 row affected (0.01 sec)

sql语句:

select a.*,(select count(1) from a2 as b where b.tid=a.id) from a1 as a;

这样就能查询a1中所有的id,对应的在a2表中出现了多少次!!

主表如何统计在附表中的出现次数?

时间: 2024-08-08 16:20:58

主表如何统计在附表中的出现次数?的相关文章

对字符串进行简单的字符数字统计 探索java中的List功能

题目: 统计一个字符串中数字和字符串的个数,并分别进行排列,要求 1.数字,字符串可以从键盘获取. 2.储存在list 3.统计数字个数,字符串个数 4.把数字和字符串按从小到大的顺序输出 5.不能使用数组. List的用法 List包括List接口以及List接口的所有实现类.因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法.[自行百度] List接口提供的

Java实现统计某字符串在另一个字符串中出现的次数

面试时会经常考这样的题目,估计也不让使用正则表达式.还好这个算法还算简单,不过在草稿纸上写难免会出现运行异常,好吧,面试官赢了,乃们屌丝就实实在在的把代码码出来吧. 下面是实现代码: /** * 统计某字符串在另一个字符串中出现的次数 * * */ public class CountHit { public static void main(String[] args) { String a = "123456abcde6ab"; String b = "6abc"

Python List count()方法-用于统计某个元素在列表中出现的次数

描述 count() 方法用于统计某个元素在列表中出现的次数. 语法 count()方法语法: list.count(obj) 参数 obj -- 列表中统计的对象. 返回值 返回元素在列表中出现的次数. 实例 以下实例展示了 count()函数的使用方法: #!/usr/bin/python aList = [123, 'xyz', 'zara', 'abc', 123]; print "Count for 123 : ", aList.count(123); print "

统计一个字符串中的单词的个数,并打印各个单词

/*测试数据:Shen zhen is a beautiful city!*/ /*运行结果:Word:6 Shen zhen is a beautiful city!*/ #include<stdio.h> #define SIZE 1000 void wordCount(char *str) { int count = 0, flag = 0; char *p = str; while (*p != '\0'){ while (*p == 32){ if (*(p + 1) == 0){/

java统计一个子串在指定字符串中出现的次数

今天查着用了用String类里的几个方法,分享下代码 题目要求:统计一个子串在指定字符串中出现的次数( 提示java字串出现了6次) 1 public class SearchSameString { 2 3 public static void main(String[] args) { 4 // 定义俩个字符串 5 String shortStr = "java"; 6 String longStr = "javasdfjavawerjavavsswetjavadfgdf

在图片上画矩形并高亮显示矩形区域、统计矩形区域中像素情况并绘制直方图

<学习OpenCV>中文版第4章第3题 提纲 题目要求 程序代码 结果图片 题目要求: ①允许用户在图片上选择一个矩形区域,当鼠标放开,高亮显示矩形区域 ②在另一个独立窗口中,使用绘图函数绘制一个图表,分别用蓝.绿和红色表示选中区域中各种颜色的像素在指定数值范围内的数量. 程序代码: 1 #include "stdafx.h" 2 #include <cv.h> 3 #include <highgui.h> 4 using namespace std

java循环练习:输入一个字符串,统计该字符串中分别包含多少个数字,多少个字母,多少个其他字符

package practiceGO; import java.util.Scanner; /*  * 3.输入一个字符串,统计该字符串中分别包含多少个数字,多少个字母,多少个其他字符  */ public class Cto { public static void main(String[] args) {         int englishCount = 0;// 英文字母个数         int spaceCount = 0;// 空格个数         int numCoun

字符串之“统计一个字符串中单词的个数”

题目:统计一个字符串中单词的个数 输入一行字符,统计其中有多少个单词,单词之间用空格分隔开 输入:my name is jacky 输出:the number of word is 4 代码如下: #include <stdio.h> int main(int argc, char *argv[]) { char str[80]; int i=0,num=0,flag=0; char c; gets(str); while((c=str[i])!='\0') { if(c==' ') flag

60.编程统计数组a中正数、0、负数的个数

#include<iostream> using namespace std; int main() { int x=0,y=0,z=0; int a[10]; cout<<"please input 10 numbers:"<<endl; for(int i=0;i<10;i++) { cin>>a[i]; } for(int j=0;j<10;j++) { if(a[j]==0) { x++; } } for(int m=