如何编写sql查询来计算下表中的对? [英] how to write sql query for counting pairs from below table?

查看:84
本文介绍了如何编写sql查询来计算下表中的对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的SQL表结构。



Below is my SQL table structure.

user_id  |   Name   |   join_side   |  left_leg   |    right_leg    |   Parent_id

100001      Tinku         Left           100002        100003              0
100002     Harish         Left           100004        100005            100001
100003     Gorav          Right          100006        100007            100001
100004      Prince        Left            100008        NULL             100002
100005      Ajay	  Right           NULL          NULL             100002
100006      Simran        Left            NULL          NULL             100003
100007      Raman	  Right	          NULL          NULL             100003
100008      Vijay	  Left	          NULL	        NULL		 100004





这是一个二进制表结构..每个用户必须在他下面添加两个id,一个是left_leg,第二个是right_leg ... Parent_id是在哪个用户当前用户下添加..希望你会理解..



我必须编写sql查询来计算id下的对100001。我知道parent_id在计算对方面会有重要作用。 *什么是对(假设任何用户同时包含left_leg和right_leg id,那么它被称为对。)

我知道在ID100001下有三对: -

1. 100002和100003

2. 100004和100005

3. 100006和100007

100008不会被算作对,因为它没有右腿..



但我不知道如何为此编写SQL查询...任何帮助将不胜感激...这是我的大学项目...而且tommorow是提交的最后日期....希望有人能帮助我...



假设我必须计算id为'100002'的对。然后在id'100002'下只有一对。即100004和100005



It is a binary table structure.. Every user has to add two per id under him, one is left_leg and second is right_leg... Parent_id is under which user current user is added.. Hope you will be understand..

I have to write sql query for counting pairs under id "100001". i know there will be important role of parent_id for counting pairs. * what is pair( suppose if any user contains both left_leg and right_leg id, then it is called pair.)
I know there are three pairs under id "100001" :-
1. 100002 and 100003
2. 100004 and 100005
3. 100006 and 100007
100008 will not be counted as pair because it does not have right leg..

But i dont know how to write sql query for this... Any help will be appreciated... This is my college project... And tommorow is the last date of submission.... Hope anyone will help me...

Suppose i have to count pair for id '100002'. Then there is only one pair under id '100002'. i.e 100004 and 100005

推荐答案

使用分组依据 [ ^ ] [ ^ ]子句。
Use Group By[^] with Having[^] clause.


这篇关于如何编写sql查询来计算下表中的对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆