如何编写Mysql查询从特定列中选择数据?

时间:2015-06-10 11:07:29

标签: php mysql

在这里,我想清楚地解释我的问题

这是我的表

events = Event.objects.all().annotate(paid_participants=models.Sum(
    models.Case(
        models.When(participant__is_paid=True, then=1),
        default=0, output_field=models.IntegerField()
    )))

请阅读以下方案。多数民众赞成我的确切问题

如果我选择一个标识id company_ID Employee_ID Name Relationship Dob Age Gender 1 EMPL 00001 Choodamani Spouse 11-Aug-66 49 Female 2 EMPL 00001 Komala Mother 30-Oct-39 76 Female 3 EMPL 00001 Varshini Daughter 29-Apr-04 11 Female 4 EMPL 00001 Vasudevan Employee 15-Jul-62 53 Male 5 EMPL 00002 Siddharth Son 1-Jun-00 15 Male 6 EMPL 00002 Poongavanam Mother 21-Oct-39 76 Female 7 EMPL 00002 Aruna Spouse 16-Sep-68 47 Female 8 EMPL 00002 Abirami Daughter 7-May-97 18 Female 9 EMPL 00002 Murali Employee 7-Oct-67 48 Male ,使用标识5我需要获取5,使用该employee_id我需要获取所有属于该employee_id的员工姓名

如何为此场景编写mysql查询

2 个答案:

答案 0 :(得分:0)

使用子查询:

SELECT * 
FROM TableName
WHERE Employee_ID=(SELECT Employee_ID 
                   FROM TableName 
                   WHERE id=5)

结果:

id  company_ID  Employee_ID Name        Relationship    Dob                         Age  Gender
5   EMPL        2           Siddharth   Son             June, 01 2000 00:00:00      15  Male
6   EMPL        2           Poongavanam Mother          October, 21 2039 00:00:00   76  Female
7   EMPL        2           Aruna       Spouse          September, 16 1968 00:00:00 47  Female
8   EMPL        2           Abirami     Daughter        May, 07 1997 00:00:00       18  Female
9   EMPL        2           Murali      Employee        October, 07 1967 00:00:00   48  Male

SQL Fiddle中的示例结果。

答案 1 :(得分:0)

您可以尝试此查询 -

SELECT * 
FROM `tablename`
WHERE `employee_id` = (SELECT `employee_id` 
                         FROM `tablename`  
                         WHERE `id` = 5)