[英]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.