简体   繁体   中英

How can I write a MySQL query to retrieve data from a table depending on other tables information?

I have these three Tables I need a query that retrieve the member.member_name of those members that are in same department and have the same stage.stage and same stage.stage_group .

department.name could be any college department name like (Art, Architecture, Electric, ....)

member.member_name could be any name like (jack, Amber, ...)

stage.stage --> (1, 2, 3, 4, 5, 6)

`stage.stage_group' --> (A, B, C, D, E)

I need some help from you to show me the way or some ideas to do that with MySQL server in the right way. So I'll be glad to see your help.

Thanks,

Use JOINs for all three tables

SELECT * FROM member
JOIN department
ON members.department_id=department.department_id
JOIN stage
ON stage.member_id=member.member_id

You can apply the inner join on three tables and if there are any conditions to be applied you can apply them in where clause

    select members.member_name from members,department,stage where
    members.department_id=department.department_id and 
    stage.member_id=members.member_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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