来自另一个表的MYSQL顺序 [英] MYSQL Order from another Table

查看:110
本文介绍了来自另一个表的MYSQL顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.

我在这里有2张桌子

表1:产品

product_id , name , images_sideview

表2:product_descriptions

table 2 : product_descriptions

product_id , volgnr

我想用表2和volgnr字段中的数字对它们进行排序.

I want to sort them with the numbers from Table 2 and volgnr field.

有没有办法用mysql做到这一点?

Is there a way to do this with mysql ?

推荐答案

有2种排序方式.升序和降序.您尚未提及订单.因此,我为您提供了两种变体形式的答案:

There are 2 ways to sort. Ascending order and Descending order. You have not mentioned the order. So I am providing you both answers with 2 variations:

升序:

SELECT DISTINCT table1.*
FROM table1
INNER JOIN table2 ON table1.product_id = table2.product_id
GROUP BY table1.product_id
ORDER BY table2.product_id ASC, table2.volgnr ASC;

降序:

SELECT DISTINCT table1.*
FROM table1
INNER JOIN table2 ON table1.product_id = table2.product_id
GROUP BY table1.product_id
ORDER BY table2.product_id DESC, table2.volgnr DESC;

如果您想让MySQL首先通过volgnr先排序 FIRST,然后再按product_id排序:

If you want to tell MySQL to first sort FIRST by volgnr and then by product_id:

升序:

SELECT DISTINCT table1.*
FROM table1
INNER JOIN table2 ON table1.product_id = table2.product_id
GROUP BY table1.product_id
ORDER BY table2.volgnr ASC, table2.product_id ASC;

降序:

SELECT DISTINCT table1.*
FROM table1
INNER JOIN table2 ON table1.product_id = table2.product_id
GROUP BY table1.product_id
ORDER BY table2.volgnr DESC, table2.product_id DESC;

希望有帮助.

我现在已经编辑了查询,以便它不会为您提供重复的结果.试试看,让我知道怎么回事.

I have now edited the query so that it does not give you duplicates in results. Try it out and let me know how that goes.

修改2: 添加了分组依据"子句.试试看.

Edit 2: Added Group By clause. Try this out.

这篇关于来自另一个表的MYSQL顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆