按一个字段选择行,并使用PHP使用mysql在多个表中按另一个字段排序

时间:2015-03-21 15:28:52

标签: php mysql

我有7个不同的表都具有相同的字段user(int)和time(int)。理想情况下,我想要一个查询将选择与特定用户匹配的所有行,然后按时间对它们进行排序。 我试过这个:http://www.w3schools.com/sql/sql_join_full.asp(加入)没有成功。我很难搜索,因为我甚至不知道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 |
+-----------+--------+--------+

1 个答案:

答案 0 :(得分:1)

我将假设每个表都遵循与您向我们展示的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列来了解该行的来源