これがテーブル構造です。MySQL のバージョンによっては、type=INNODB を ENGINE=INNODB に変更する必要がある場合があることに注意してください。
drop table if exists prjusers;
drop table if exists proj_users;
drop table if exists project_regions;
drop table if exists projects;
drop table if exists regions;
/*==============================================================*/
/* Table: prjusers */
/*==============================================================*/
create table prjusers
(
id_prjuser int not null auto_increment,
id_region int not null,
user_name varchar(100) not null,
primary key (id_prjuser)
)
type = InnoDB;
/*==============================================================*/
/* Table: proj_users */
/*==============================================================*/
create table proj_users
(
id_proj_user int not null auto_increment,
id_prjuser int not null,
id_project int not null,
primary key (id_proj_user)
)
type = InnoDB;
/*==============================================================*/
/* Table: project_regions */
/*==============================================================*/
create table project_regions
(
id_project_region int not null auto_increment,
id_project int not null,
id_region int not null,
primary key (id_project_region)
)
type = InnoDB;
/*==============================================================*/
/* Table: projects */
/*==============================================================*/
create table projects
(
id_project int not null auto_increment,
prj_code varchar(100) not null,
global_project char(1) not null,
primary key (id_project)
)
type = InnoDB;
/*==============================================================*/
/* Table: regions */
/*==============================================================*/
create table regions
(
id_region int not null auto_increment,
region_name varchar(100) not null,
primary key (id_region)
)
type = InnoDB;
alter table prjusers add constraint FK_relationship_12 foreign key (id_region)
references regions (id_region) on delete restrict on update restrict;
alter table proj_users add constraint FK_relationship_10 foreign key (id_prjuser)
references prjusers (id_prjuser) on delete restrict on update restrict;
alter table proj_users add constraint FK_relationship_11 foreign key (id_project)
references projects (id_project) on delete restrict on update restrict;
alter table project_regions add constraint FK_relationship_8 foreign key (id_project)
references projects (id_project) on delete restrict on update restrict;
alter table project_regions add constraint FK_relationship_9 foreign key (id_region)
references regions (id_region) on delete restrict on update restrict;
選択ステートメント:
select id_project, prj_code from projects where global_project='Y'
UNION
select projects.id_project, projects.prj_code
from projects join project_regions on projects.id_project = project_regions.id_project
join prjusers on prjusers.id_region = project_regions.id_region
where prjusers.id_prjuser = {$_SESSION['id_prjuser']}
UNION
select projects.id_project, projects.prj_code
from projects join proj_users on projects.id_project = proj_users.id_project
where proj_users.id_prjuser = {$_SESSION['id_prjuser']}
上記は 3 つの UNIONS を実行し、プロジェクト ID とプロジェクト コード (1) はグローバル (1)、ID がセッション変数 (2) に保持されているログイン ユーザーの領域に分類され、すべてのプロジェクトは明示的に生成されます。そのユーザーに割り当てられます。