Join MYSQL tables and sort by two fields?

两盒软妹~` 提交于 2019-12-11 00:45:20

问题


I'm having a problem sorting my tables in MYSQL.

I have my tables setup like this:

Order_details

  • Order_ID
  • shipping_cost
  • printed

Product_details

  • ID
  • Order_ID
  • SKU_location

I want to SELECT all orders WHERE printed = FALSE but also sort by the shipping costs and THEN by the SKU_location

How can I join the tables into one query so it sorts by shipping_cost and SKU_location where printed = false?


回答1:


You can do an implicit JOIN in the following way:

SELECT * 
FROM Order_details od
JOIN Product_details pd
ON od.Order_ID = pd.Order_ID
WHERE od.printed = FALSE
ORDER BY od.shipping_cost, pd.SKU_location

The text following each table renames the table for easy reference in the later parts of the query (i.e. the code "Database.Long_Table_Name ltn" renames the table to "ltn")



来源:https://stackoverflow.com/questions/5068714/join-mysql-tables-and-sort-by-two-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!