从MySQL中的多个表中对产品数据进行排序

时间:2016-12-27 09:40:26

标签: php mysql sorting

我有两张桌子。第一个:

Products table:

+----+-----------+
| id | name      | 
+----+-----------+
|  1 | Product 1 |
|  2 | Product 2 |
|  3 | Product 3 |
+----+-----------+

这包含产品名称和其他表格包含不同产品变体的价格:

Products prices table:

+-----+------------+-------------+
| id  | product_id | price       |
+-----+------------+-------------+
|  5  | 1          |  12.00      | 
|  6  | 1          |  32.00      | 
|  11 | 1          |  56.00      | 
|  14 | 2          |  11         | 
|  44 | 3          |  12         |
+-----+------------+-------------+

我需要在价格上创建一个排序(最低和最高)

3 个答案:

答案 0 :(得分:0)

是的,你可以,为此你必须使用 JOIN ORDER BY 条款。

例如:

select t1.id, t2.product_id t2.price FROM table1 t1 JOIN table2 t2 ON t1.id = t2.pid ORDER BY t2.price

Mysql join with order by reference guide

答案 1 :(得分:0)

示例查询:

 select t1.[id],t1.[name],t2.[price]
 from [yourtable1] t1
 Join [yourtable2] t2 on
 t1.[id]=t2.[product_id]
 Order by t2.[price] Asc;

答案 2 :(得分:0)

你可以尝试这段代码......

 SELECT product_tbl.name, product_tbl.id, product_price.product_id, product_price.price FROM product_tbl, product_price WHERE product_tbl.id=product_price.product_id ORDER BY product_price.price DESC;