简体   繁体   中英

Check a record exists in mysql

Assume I have following three tables.

Staff 

id | name
---------
1    Tom
2    Mary
Fulltime staff

id | name
---------
1    Tom
Parttime staff

id | name
---------
2    Mary 

Is there a way I can display like this using SQL? Or should I add one more column in the Staff table?

Mysql

id | name | Work nature
----------------------
1    Tom    Fulltime
2    Mary   Parttime

Unless you want people to have different names when they're full time vs part time, do not repeat people's names.

There's nothing preventing someone from being both full and part time. Or neither. Instead of three tables, have one.

create table staff (
  id bigint primary key auto_increment,
  name varchar(255) not null,
  type enum('fulltime', 'parttime') not null
)

Now each person can only be one type of employee, the names are not repeated, and your select is trivial.

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