MYSQL 从列表中按字段排序 table
MYSQL ORDER BY FIELD from a list from and other table
这个问题我绞尽脑汁想了很久,不知道怎么解决。
我有2个mysqltables
表 1
+----+----------------------+
| id | name | order_list |
+----+----------------------+
| 1 | Bob | 3 |
| 2 | Marc | 2 |
| 3 | Edith | 1 |
| 4 | John | 4 |
+----+----------------------+
表 2
+----+-------------------+
| id | user_id | sale |
+----+-------------------+
| 1 | 3 | 30 |
| 2 | 2 | 40 |
| 3 | 1 | 100 |
| 4 | 3 | 75 |
| 5 | 2 | 40 |
| 6 | 1 | 100 |
| 7 | 4 | 75 |
+----+-------------------+
我需要从 table2 订购所有来自 table 的 order_list 1 或来自 table 2 的 user_id。
现在我正在 PHP 中这样做。
1 个查询以从 table 1 个订单 order_list 获取员工 ID。
一个循环
在每个员工的 1 个查询中获得销售
我需要每个销售都是独立的,而不是求和或分组。
query to get the id from table 1 order by order_list
loop
query the table2 for the sale of the looped employee
end loop
我知道这可以用这样的东西来完成。
SELECT * FROM table2 ORDER BY FIELD (id,order from table 1)
SELECT a.* FROM table2 a
LEFT JOIN table1 b on a.user_id = b.id
ORDER BY b.order_list
只需加入它们,您就可以访问 2 个表中的列。
一个基本的join
就足够了:
select t2.*
from table2 t2 join
table1 t1
on t2.user_id = t1.id
order by t1.order_list;
或者,如果您愿意,可以在 order by
:
中放置一个子查询
select t2.*
from table2 t2
order by (select t1.order_list from table1 t1 where t1.id = t2.user_id);
这个问题我绞尽脑汁想了很久,不知道怎么解决。
我有2个mysqltables
表 1
+----+----------------------+
| id | name | order_list |
+----+----------------------+
| 1 | Bob | 3 |
| 2 | Marc | 2 |
| 3 | Edith | 1 |
| 4 | John | 4 |
+----+----------------------+
表 2
+----+-------------------+
| id | user_id | sale |
+----+-------------------+
| 1 | 3 | 30 |
| 2 | 2 | 40 |
| 3 | 1 | 100 |
| 4 | 3 | 75 |
| 5 | 2 | 40 |
| 6 | 1 | 100 |
| 7 | 4 | 75 |
+----+-------------------+
我需要从 table2 订购所有来自 table 的 order_list 1 或来自 table 2 的 user_id。
现在我正在 PHP 中这样做。 1 个查询以从 table 1 个订单 order_list 获取员工 ID。 一个循环 在每个员工的 1 个查询中获得销售
我需要每个销售都是独立的,而不是求和或分组。
query to get the id from table 1 order by order_list
loop
query the table2 for the sale of the looped employee
end loop
我知道这可以用这样的东西来完成。
SELECT * FROM table2 ORDER BY FIELD (id,order from table 1)
SELECT a.* FROM table2 a
LEFT JOIN table1 b on a.user_id = b.id
ORDER BY b.order_list
只需加入它们,您就可以访问 2 个表中的列。
一个基本的join
就足够了:
select t2.*
from table2 t2 join
table1 t1
on t2.user_id = t1.id
order by t1.order_list;
或者,如果您愿意,可以在 order by
:
select t2.*
from table2 t2
order by (select t1.order_list from table1 t1 where t1.id = t2.user_id);