繁体   English   中英

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

[英]How to write a Mysql Query to select data from specific column?

在这里我想清楚地说明我的问题

这是我的桌子

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

请阅读以下情况。 多数民众赞成在我的确切问题

在这里,如果我选择一个id 5 ,使用id 5我需要获取employee_id ,使用那个employee_id我需要获取所有都属于该employee_id的雇员的姓名

我如何针对这种情况编写mysql查询

使用子查询:

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中的示例结果。

您可以尝试此查询-

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

暂无
暂无

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

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