drvpvmz16241016 2012-03-20 13:44
浏览 5
已采纳

来自另一个表的MYSQL订单

I have a question.

I have here 2 tables

table 1 : products

product_id , name , images_sideview

table 2 : product_descriptions

product_id , volgnr

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

Is there a way to do this with mysql ?

  • 写回答

3条回答 默认 最新

  • duanchendu69495 2012-03-20 13:57
    关注

    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:

    ASCENDING ORDER:

    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;
    

    DESCENDING ORDER:

    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;
    

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

    ASCENDING ORDER:

    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;
    

    DESCENDING ORDER:

    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;
    

    Hope that helps.

    Edit 1:

    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.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?