2014-02-25 31 views
0

我有表documentskeywords。每個文檔可以有任意數量的關鍵字。我怎樣才能'按'非標準化列?和複合訂單

關鍵字例子:

Client NameBilling periodClient Address

必須注意的是,文件可以對任何關鍵字,所以它不能被歸一化(它只是元數據)。

現在我想訂購,例如,Client Name,然後Billing Period,我該怎麼辦?

如果我讓Select * from Keywords group by keyword order by value我沒有得到我所需要的。我沒有太多的MySQL經驗,所以我不能做更多。

實例與測試數據:

+-------+-------------+----------------------+----------------------+ 
| id | document_id | keyword    | value    | 
+-------+-------------+----------------------+----------------------+ 
| 265 |   89 | Nº de Operacion  | 000534556315   | 
| 15708 |  5234 | Direccion IP   | 192.168.100.168  | 
| 267 |   89 | Fecha    | 20131021    | 
| 15760 |  5240 | Nombre de Cliente | CLIENTEN1   | 
| 15761 |  5240 | Asunto    | DEMANDACESTADO1220 | 
| 15703 |  5234 |      | DEMANDACESTADO1220 | 
| 15700 |  5234 | Nombre de Legajo  | Documento   | 
| 15702 |  5234 | Nombre del Documento | Documento   | 
| 15701 |  5234 | Tipo de Documento | Documento   | 
| 15842 |  5256 | Descripcion   | ffff     | 
| 15709 |  5234 | Localizacion   | No definida   | 
| 15707 |  5234 | Grupo Usuario  | Operadores   | 
| 266 |   89 | Socio    | Socio1    | 
| 15835 |  5255 | Decripcion   | sadsdf    | 
| 15704 |  5234 | ID de Usuario  | ssadmin    | 
| 15706 |  5234 | Nombre de Usuario | ssadmin    | 
+-------+-------------+----------------------+----------------------+ 

mysql> describe documents; 
+---------+-----------+------+-----+---------------------+-----------------------------+ 
| Field | Type  | Null | Key | Default    | Extra      | 
+---------+-----------+------+-----+---------------------+-----------------------------+ 
| id  | int(11) | NO | PRI | NULL    | auto_increment    | 
| name | char(100) | YES |  | NULL    |        | 
| wfid | char(50) | YES |  | NULL    |        | 
| docid | char(50) | YES |  | NULL    |        | 
| created | timestamp | NO |  | 0000-00-00 00:00:00 |        | 
| updated | timestamp | NO |  | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | 
+---------+-----------+------+-----+---------------------+-----------------------------+ 
6 rows in set (0.00 sec) 

mysql> describe keywords; 
+-------------+--------------+------+-----+---------+----------------+ 
| Field  | Type   | Null | Key | Default | Extra   | 
+-------------+--------------+------+-----+---------+----------------+ 
| id   | int(11)  | NO | PRI | NULL | auto_increment | 
| document_id | int(11)  | NO | MUL | NULL |    | 
| keyword  | char(50)  | NO |  | NULL |    | 
| value  | varchar(250) | YES |  | NULL |    | 
+-------------+--------------+------+-----+---------+----------------+ 
4 rows in set (0.00 sec) 
+0

對於您顯示的查詢,您會得到什麼? '通過關鍵字按值排序,從關鍵字組中選擇*。如果你把它完全寫在你的問題上會更好。 –

+0

據我所知OP是要求動態排序的值(來自keywords.keyword) – jean

+1

您的結構是衆所周知的EAV。在絕大多數關係數據庫的情況下,這是不好的選擇。我已經描述過[這裏]的原因(http://stackoverflow.com/a/20783125/2637490)。 –

回答

1

你的 「關鍵詞」 是不是關鍵字。它們是屬性值對。這種數據存儲方式稱爲「實體 - 屬性 - 值」(縮寫爲EAV)。我不建議將它用於每個文檔上的屬性。

無論如何,這是你的。你可以做你想要使用聚合有條件聚集了什麼:

select document_id, 
     max(case when keyword = 'Client Name' then value end) as ClientName, 
     max(case when keyword = 'Billing Period' then value end) as BillingPeriod 
from keywords 
group by document_id 
order by ClientName, BillingPeriod; 
0

目前還不清楚是否可能有一個以上的客戶名稱和/或每份文檔不止一個結算週期。如果你做到了,那麼以下內容將達到你想要的。然而,正如其他人所說,這是一種將結構化信息存儲在數據庫中的非常糟糕的方式。

select d.id, cn.ClientName, bp.BillingPeriod from documents d 
left join (select document_id, value as ClientName from keywords where keyword = 'Client Name') cn on d.id = cn.document_id 
left join (select document_id, value as BillingPeriod from keywords where keyword = 'Billing Period') bp on d.id = bp.document_id 
group by name, cn.ClientName, bp.BillingPeriod, d.id 
order by cn.ClientName, bp.BillingPeriod 
+0

嗨,文件將只有一個關鍵與例如「客戶名稱」,密鑰不重複每個文件。 – JorgeeFG

+0

然後,我建議您通過在關鍵字表中的document_id +關鍵字上添加唯一索引來強制執行此操作 – jksemple