簡體   English   中英

使用PHP使用mysql按一個字段選擇行並在多個表中按另一個排序

[英]Select rows by one field and sort by another across multiple tables with mysql using PHP

我有7個不同的表,它們都具有相同的字段, user (int)和time (int)。 理想情況下,我需要一個查詢,該查詢將選擇與特定用戶匹配的所有行,然后按時間對所有行進行排序。 我已經嘗試過: http : //www.w3schools.com/sql/sql_join_full.asp (JOIN FULL)沒有成功。 我很難搜尋,因為我什至不知道這里是否需要JOIN。

像這樣:

SELECT * FROM table1,table2,table3 WHERE user='1' ORDER BY time DESC

會給我這個:

//my query and a while loop here
    $row['food'];//[{"food" : "burrito","qty" : "1" }]
    $row['feel'];//8
    //next row
    $row['ailment'];// xx some value xx
    $row['feel'];//4
    //etc

這是表的一些示例-我也有一個用戶表。

+----+------------+------------------+------+------+---------+
| id | time       | ailments         | user | feel | comment |
+----+------------+------------------+------+------+---------+
|  1 | 1426870546 | xx some value xx | 1    |    4 |         |
|  2 | 1426870577 | xx some value xx | 1    |    4 |         |
|  3 | 1426870881 | xx some value xx | 1    |    8 |         |
|  4 | 1426877198 | xx some value xx | 5    |    8 |         |
|  5 | 1426881122 | xx some value xx | 2    |    2 |         |
|  6 | 1426881778 | xx some value xx | 6    |    8 |         |
+----+------------+------------------+------+------+---------+

+----+------------+-------------------------------------+------+------+---------+
| id | time       | food                                | user | feel | comment |
+----+------------+-------------------------------------+------+------+---------+
|  1 | 1426860681 | [{"food" : "burrito","qty" : "1" }] | 2    |    8 |         |
|  2 | 1426861024 | [{"food" : "burrito","qty" : "1" }] | 1    |    8 |         |
|  3 | 1426861043 | [{"food" : "burrito","qty" : "1" }] | 5    |    8 |         |
|  4 | 1426861069 | [{"food" : "burrito","qty" : "1" }] | 2    |    8 |         |
|  5 | 1426882559 | [{"food" : "taco","qty" : "1" }]    | 6    |    7 |         |
|  6 | 1426884089 | [{"food" : "taco","qty" : "1" }]    | 6    |    7 |         |
|  7 | 1426884487 | [{"food" : "taco","qty" : "1" }]    | 6    |    7 |         |
+----+------------+-------------------------------------+------+------+---------+

編輯:用戶表,另一個表中的user是此一個中的id

+----+------+--------+--------+
| id | user |  pass  | email  |
+----+------+--------+--------+
|  1 | jim  | xxxxx  | j@b.ca |
|  4 | otto | xxxxx  | o@b.ca |
|  5 | tat  | xxxxx  | t@b.ca |
|  6 | al   | xxxxx  | a@b.ca |
+-----------+--------+--------+

我將假設每個表都遵循與您顯示給我們的2個模式相同的模式:id,時間,用戶,感覺,評論,以及1個以其他方式命名的列(在這些情況下為食物和疾病)。 為了清楚起見,我將命名其他列A,B等。我將在每個表的“特殊”列之后命名。 (下一次給我們一些表名...)

SELECT id, time, ailments AS special, user, feel, comment, 'ailments' as src
FROM table_ailments
WHERE user = 1
UNION ALL
SELECT id, time, food as special, user, feel, comment, 'food' as src
FROM table_food
WHERE user = 1
UNION ALL
SELECT id, time, A as special, user, feel, comment, 'A' as src
FROM table_A
WHERE user = 1
UNION ALL
SELECT id, time, B as special, user, feel, comment, 'B' as src
FROM table_B
WHERE user = 1
UNION ALL
SELECT id, time, C as special, user, feel, comment, 'C' as src
FROM table_C
WHERE user = 1
ORDER by user, time

這將為您提供按用戶排序的所有表中的所有行,然后按時間排序,您可以通過查看src列來確定行的來源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM