2014-02-26 73 views
3

如何在這裏按價值排序?我需要按照值的順序獲取文檔ID(如果存在)。結果查詢在EAV表中按值排序的MySQL?

例子:

+------+------+------------+-------+---------------------+---------------------+ 
| id | name | wfid  | docid | created    | updated    | 
+------+------+------------+-------+---------------------+---------------------+ 
| 5269 | DOC2 | documentos | doc | 2014-02-25 09:34:56 | 2014-02-25 09:39:51 | 
| 5270 | DOC2 | documentos | doc | 2014-02-25 09:34:57 | 2014-02-25 10:41:57 | 
| 5271 | DOC2 | documentos | doc | 2014-02-25 09:34:57 | 2014-02-25 10:42:20 | 
+------+------+------------+-------+---------------------+---------------------+ 

我不知道如何去實現它。它適用於一個非常難看的查詢,但無法排序。例如,我希望所有的文件都是從Client1開始,並通過Period訂購。用戶必須有權查看文檔,權限由wfiddociduserid組成。

可能嗎?

小提琴:http://sqlfiddle.com/#!2/3b49e8

模式:

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 users; 
+----------+----------+------+-----+---------+----------------+ 
| Field | Type  | Null | Key | Default | Extra   | 
+----------+----------+------+-----+---------+----------------+ 
| id  | int(11) | NO | PRI | NULL | auto_increment | 
| login | char(30) | NO |  | NULL |    | 
| password | char(50) | NO |  | NULL |    | 
+----------+----------+------+-----+---------+----------------+ 
3 rows in set (0.00 sec) 

mysql> describe permissions; 
+--------+----------+------+-----+---------+----------------+ 
| Field | Type  | Null | Key | Default | Extra   | 
+--------+----------+------+-----+---------+----------------+ 
| id  | int(11) | NO | PRI | NULL | auto_increment | 
| wfId | char(50) | NO |  | NULL |    | 
| docId | char(50) | NO |  | NULL |    | 
| userId | int(11) | NO | MUL | NULL |    | 
+--------+----------+------+-----+---------+----------------+ 
4 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.01 sec) 

數據:

mysql> select * from permissions; 
+----+------------+-------+--------+ 
| id | wfId  | docId | userId | 
+----+------------+-------+--------+ 
| 13 | documentos | doc |  7 | 
+----+------------+-------+--------+ 
1 row in set (0.00 sec) 

mysql> select * from users; 
+----+-------+----------+ 
| id | login | password | 
+----+-------+----------+ 
| 7 | admin | a  | 
+----+-------+----------+ 
1 row in set (0.00 sec) 

mysql> select * from documents; 
+------+------+------------+-------+---------------------+---------------------+ 
| id | name | wfid  | docid | created    | updated    | 
+------+------+------------+-------+---------------------+---------------------+ 
| 5263 | DOC1 | documentos | doc | 2014-02-25 09:34:44 | 2014-02-25 09:38:09 | 
| 5264 | DOC1 | documentos | doc | 2014-02-25 09:34:50 | 2014-02-25 09:38:25 | 
| 5265 | DOC1 | documentos | doc | 2014-02-25 09:34:50 | 2014-02-25 09:38:37 | 
| 5266 | DOC1 | documentos | doc | 2014-02-25 09:34:50 | 2014-02-25 09:38:59 | 
| 5267 | DOC1 | documentos | doc | 2014-02-25 09:34:51 | 2014-02-25 09:39:10 | 
| 5268 | DOC2 | documentos | doc | 2014-02-25 09:34:56 | 2014-02-25 09:39:25 | 
| 5269 | DOC2 | documentos | doc | 2014-02-25 09:34:56 | 2014-02-25 09:39:51 | 
| 5270 | DOC2 | documentos | doc | 2014-02-25 09:34:57 | 2014-02-25 10:41:57 | 
| 5271 | DOC2 | documentos | doc | 2014-02-25 09:34:57 | 2014-02-25 10:42:20 | 
| 5272 | DOC2 | documentos | doc | 2014-02-25 09:34:58 | 2014-02-25 10:43:04 | 
+------+------+------------+-------+---------------------+---------------------+ 
10 rows in set (0.00 sec) 

mysql> select * from keywords; 
+-------+-------------+-----------+----------+ 
| id | document_id | keyword | value | 
+-------+-------------+-----------+----------+ 
| 15888 |  5263 | Nombre | Cliente1 | 
| 15889 |  5264 | Nombre | Cliente1 | 
| 15890 |  5265 | Nombre | Cliente1 | 
| 15891 |  5266 | Nombre | Cliente2 | 
| 15892 |  5267 | Nombre | Cliente3 | 
| 15893 |  5268 | Nombre | Cliente3 | 
| 15894 |  5269 | Nombre | Cliente3 | 
| 15895 |  5270 | Nombre | Cliente4 | 
| 15896 |  5271 | Nombre | Cliente4 | 
| 15897 |  5272 | Nombre | Cliente4 | 
| 15898 |  5263 | Periodo | 201301 | 
| 15899 |  5263 | OtroValor | 1  | 
| 15900 |  5264 | Periodo | 201301 | 
| 15901 |  5264 | OtroValor | 1  | 
| 15902 |  5265 | Periodo | 201301 | 
| 15903 |  5265 | OtroValor | 1  | 
| 15904 |  5266 | Periodo | 201302 | 
| 15905 |  5266 | OtroValor | 2  | 
| 15906 |  5267 | Periodo | 201302 | 
| 15907 |  5267 | OtroValor | 2  | 
| 15908 |  5268 | Periodo | 201302 | 
| 15909 |  5268 | OtroValor | 2  | 
| 15910 |  5269 | Periodo | 201303 | 
| 15911 |  5269 | OtroValor | 3  | 
| 15912 |  5270 | Periodo | 201303 | 
| 15913 |  5270 | OtroValor | 3  | 
| 15914 |  5271 | Periodo | 201304 | 
| 15915 |  5271 | OtroValor | 3  | 
| 15916 |  5272 | Periodo | 201304 | 
+-------+-------------+-----------+----------+ 
29 rows in set (0.00 sec) 
+0

考慮提供適當的DDL(和/或sqlfiddle)與期望的結果集 – Strawberry

+0

http:// sqlfiddle .com /#!2/3b49e8 – JorgeeFG

+0

如果我們從這樣的事情開始,是否有幫助... http://sqlfiddle.com/#!2/3b49e8/1 ...? – Strawberry

回答

1

我覺得我做的伎倆,所以我們去:

首先,你必須INNER加入docspermissions表條件喲你想(相同的docid和相同的wfid),以獲得只有docs有權限的行。然後,按用戶進行過濾,只取得您想要的用戶。我把條件放在最後,但是如果你願意的話,你可以包含在ON條件中,以便稍後加入表格時提高性能。之後,你把你想要的訂單條件。

我結束了在SQL是:

SELECT d.id, d.name, d.wfid, d.docid, d.created, d.updated 
FROM documents d 
INNER JOIN permissions p 
ON (d.wfid = p.wfId 
    AND d.docid = p.docId) 
WHERE p.userid = 7 
ORDER BY d.created DESC 

http://sqlfiddle.com/#!2/3b49e8/14/0

,如果你提供的createdwfid指數和docsdocid領域,以提高性能這可能是不錯的,因爲你使用它們作爲內部連接和訂購的關鍵。

UPDATE

檢查:http://sqlfiddle.com/#!2/3b49e8/19/0

我已經添加了其他表中查詢,使得兩次參加過關鍵字:

SELECT d.id, d.name, d.wfid, d.docid, d.created, d.updated, k1.keyword 
FROM documents d 
INNER JOIN permissions p 
ON (d.wfid = p.wfId 
    AND d.docid = p.docId) 
INNER JOIN keywords k1 
ON (k1.document_id = d.id 
    AND k1.keyword = 'Periodo') 

INNER JOIN keywords k2 
ON (k2.document_id = d.id 
    AND k2.keyword = 'Nombre') 

WHERE p.userid = 7 
AND k2.value = 'Cliente1' 

ORDER BY d.created DESC 

正如你所看到的,我添加了加入關鍵字以顯示它爲「Periodo」

+0

嘿謝謝你的回答,我需要的是通過'keywords'表中的值進行排序,這裏您正在使用「文件」表格進行訂購。 – JorgeeFG

+0

啊,我沒有看到它的問題。請更新它,因爲您沒有提及任何有關關鍵字的內容,以及它與其他表格的關係。如果您希望該表格進行排序,則必須加入我上面所寫的查詢。 – Chococroc

+0

好吧,我認爲它現在就是你想要的; D – Chococroc