简体   繁体   English

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

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

I have 7 different tables that all have the same field, user (int) and time (int). 我有7个不同的表,它们都具有相同的字段, user (int)和time (int)。 Ideally, I want one query that will select all the rows that match a specific user, then sort them all by time. 理想情况下,我需要一个查询,该查询将选择与特定用户匹配的所有行,然后按时间对所有行进行排序。 I've tried this: http://www.w3schools.com/sql/sql_join_full.asp (JOIN FULL) with no success. 我已经尝试过: http : //www.w3schools.com/sql/sql_join_full.asp (JOIN FULL)没有成功。 I'm having a hard time searching because I don't even know if a JOIN is what I need here. 我很难搜寻,因为我什至不知道这里是否需要JOIN。

Something like this: 像这样:

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

Would get me this: 会给我这个:

//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

Here's some examples of the tables - I have a users table too. 这是表的一些示例-我也有一个用户表。

+----+------------+------------------+------+------+---------+
| 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 |         |
+----+------------+------------------+------+------+---------+

and

+----+------------+-------------------------------------+------+------+---------+
| 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 |         |
+----+------------+-------------------------------------+------+------+---------+

EDIT: Users table, the user in the other table is the id in this one 编辑:用户表,另一个表中的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 |
+-----------+--------+--------+

I'm going to assume that each table follows the same pattern as the 2 that you've shown us: id, time, user, feel, comment, plus 1 otherwise-named column (food and ailments in these cases). 我将假设每个表都遵循与您显示给我们的2个模式相同的模式:id,时间,用户,感觉,评论,以及1个以其他方式命名的列(在这些情况下为食物和疾病)。 To be clear I will name the other columns A, B, etc. I will name each table after it's "special" column. 为了清楚起见,我将命名其他列A,B等。我将在每个表的“特殊”列之后命名。 (Next time give us some table names...) (下一次给我们一些表名...)

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

This will give you all rows from all tables ordered by user and then by time and you can tell where the row came from by looking at the src column 这将为您提供按用户排序的所有表中的所有行,然后按时间排序,您可以通过查看src列来确定行的来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM