如何在這裏按價值排序?我需要按照值的順序獲取文檔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
訂購。用戶必須有權查看文檔,權限由wfid
,docid
和userid
組成。
可能嗎?
小提琴: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)
考慮提供適當的DDL(和/或sqlfiddle)與期望的結果集 – Strawberry
http:// sqlfiddle .com /#!2/3b49e8 – JorgeeFG
如果我們從這樣的事情開始,是否有幫助... http://sqlfiddle.com/#!2/3b49e8/1 ...? – Strawberry