0

だから私は人々がグループに入れられ、それらのグループが各人に保存されるプロパティを決定するデザインを持っています。

したがって、後で VIP 顧客を獲得し、その VIP 番号を記録することにした場合は、プロパティ「VIP_Number」を使用して「VIP メンバー」という新しいグループを作成し、それらの顧客をそのグループに配置できます。

したがって、従業員やその他の顧客には、関係のないフィールドはありません。

特定の人物の個別のテーブルから必要なすべてのデータを取得する最良の方法は何ですか? 後日追加されるデータ テーブルをサポートするソリューションが必要です。

テーブルのデザインはこちら。

ノート:

  • 「d」プレフィックスはデータ テーブル用です
  • 「x」プレフィックスは相互参照テーブル用です
  • 「p」プレフィックスはプロパティ用です
  • 読みやすくするために、さまざまな長さの ID を使用しました
  • John Smith は従業員であるため、個人でもあります。
  • Anna Backhouse は顧客であり、したがって個人でもあります。
  • Mr Both は従業員と顧客の両方であり、したがって個人でもあります。

dピープル

ID                   pFirst_Name          pLast_Name
----------------------------------------------------------
001                  John                 Smith
002                  Anna                 Backhouse
003                  Mr                   Both

d従業員

ID                   pBadge_Code
----------------------------------------------------------
01                   MB2012
02                   JS2012

dお客様

ID                   pPhone_Number
----------------------------------------------------------
01                   1800 backhouse
02                   1800 both

dGROUP

ID                   pGroup_Name
----------------------------------------------------------
1                    People
2                    Employees
3                    Customers

xGROUPS

dGROUPS_ID_parent    dGROUPS_ID_child
----------------------------------------------------------
1                    1
1                    2
1                    3

xピープル

dPeople_ID           dGROUPS_ID
----------------------------------------------------------
001                  2
002                  3
003                  2
003                  3

x従業員

dPeople_ID           dEmployees_ID
----------------------------------------------------------
001                  02
003                  01

xお客様

dPeople_ID           dCustomers_ID
----------------------------------------------------------
002                  01
003                  02

Mr Both を検索した場合の望ましい結果:

ID    pFirst_Name  pLast_Name  pBadge_Code  pPhone_Number
----------------------------------------------------------
003   Mr           Both        MB2012       1800 both

John Smith を検索した場合の望ましい結果:

ID    pFirst_Name  pLast_Name  pBadge_Code  
----------------------------------------------------------
001   John         Smith       JS2012     

Anna Backhouse を検索した場合の望ましい結果:

ID    pFirst_Name  pLast_Name  pPhone_Number
----------------------------------------------------------
002   Anna         Backhouse   1800 backhouse

彼らが使用しているすべてのグループのリストを取得できます。

SELECT `dGROUPS`.`ID` AS `Group ID`, `dGROUPS`.`pGroup_Name` AS `Group Name` FROM `dGROUPS`, `xGROUPS` WHERE `xGROUPS`.`dGROUPS_ID_parent` = `dGROUPS`.`ID` AND (`xGROUPS`.`dGROUPS_ID_child` IN (SELECT `dGROUPS_ID` FROM `xPeople` WHERE dPeople_ID = 003))

これは、People > Employees > Managers などのサブグループがある場合に役立ちます。

このクエリを実行し、それを使用して別のクエリを動的に形成して、目的の結果を得る必要があると考えています。2 つのクエリを使用してもかまいませんが、1 つを使用することをお勧めします。

おそらく、ビューを使用した解決策はありますか?

PHPとMySQLを使用しています。

4

2 に答える 2

0

If you don't mind about having empty columns (you could handle them in the php side) you can get the desired result without using dynamic queries. This would also allow you to throw away grouping tables: dGROUPS and xGROUPS don't give any added value for just this purpose the relation among entities is already given by the actual data, they only generate Update anomalies.

This a sample made with postgres (but you can do the same with mysql).

postgres=# with dPeople(ID, pFirst_Name, pLast_Name) as (
postgres(#              values('001','John','Smith')
postgres(#              union all
postgres(#              values('002','Anna','Backhouse')
postgres(#              union all
postgres(#              values('003','Mr','Both')
postgres(#      ), dEmployees(ID, pBadge_Code) as (
postgres(#              values('01','MB2012')
postgres(#              union all
postgres(#              values('02','JS2012')
postgres(#      ), dCustomers (ID, pPhone_Number) as (
postgres(#              values('01','1800 backhouse')
postgres(#              union all
postgres(#              values('02','1800 both')
postgres(#      ), xPeople (dPeople_ID, dGROUPS_ID) as (
postgres(#              values('001','2')
postgres(#              union all
postgres(#              values('002','3')
postgres(#              union all
postgres(#              values('003','2')
postgres(#              union all
postgres(#              values('003','3')
postgres(#      ), xEmployees (dPeople_ID, dEmployees_ID) as (
postgres(#              values('001','02')
postgres(#              union all
postgres(#              values('003','01')
postgres(#      ), xCustomers (dPeople_ID, dCustomers_ID) as (
postgres(#              values('002','01')
postgres(#              union all
postgres(#              values('003','02')
postgres(#      )
postgres-# select *
postgres-# from (
postgres(#              select ID as dPeople_ID, pFirst_Name, pLast_Name
postgres(#              from dPeople
postgres(#      ) dPeople
postgres-#      left outer join (
postgres(#                      select dPeople_ID, dEmployees_ID, pBadge_Code
postgres(#                      from xEmployees
postgres(#                              join (
postgres(#                                              select ID as dEmployees_ID, pBadge_Code
postgres(#                                              from dEmployees
postgres(#                                      ) as dEmployees using (dEmployees_ID)
postgres(#              ) as Employees using (dPeople_ID)
postgres-#      left outer join (
postgres(#                      select dPeople_ID, dCustomers_ID,  pPhone_Number
postgres(#                      from xCustomers
postgres(#                              join (
postgres(#                                              select ID as dCustomers_ID, pPhone_Number
postgres(#                                              from dCustomers
postgres(#                                      ) as Customers using (dCustomers_ID)
postgres(#              ) as Customers using (dPeople_ID)
postgres-# ;
 dpeople_id | pfirst_name | plast_name | demployees_id | pbadge_code | dcustomers_id | pphone_number
------------+-------------+------------+---------------+-------------+---------------+----------------
 001        | John        | Smith      | 02            | JS2012      |               |
 002        | Anna        | Backhouse  |               |             | 01            | 1800 backhouse
 003        | Mr          | Both       | 01            | MB2012      | 02            | 1800 both
(3 rows)
于 2012-07-20T12:16:47.817 に答える
0

select 句を変更し、アスタリスクですべての列を選択し、後でテーブルに追加された新しい列も変更しました。

両方氏:

  --select dp.ID, dp.pFirst_Name, dp.pLast_Name, de.pBadge_Code, dc.pPhone_Number
    select dp.*, dp.*, dp.*, de.*, dc.*
    from dPeople dp join xCustomers xc
      on dp.id = xc.dPeople_ID
    join dCustomers dc
      on xc.dCustomers_ID = dc.ID
    join xEmployees xe
      on dp.id = xe.dPeople_ID
    join dEmployees de
      on  xe.dEmployees_ID = de.ID
于 2012-07-20T07:52:16.213 に答える