开发者

Order by in mysql using second table

开发者 https://www.devze.com 2022-12-09 14:45 出处:网络
I have two tables, one is a list os stores and attributes, the second is a list of allocationsa based on these attributes.

I have two tables, one is a list os stores and attributes, the second is a list of allocationsa based on these attributes. The 开发者_运维百科attribute table (stores_metadata)

| key | store_key | field | value
| 1   | 1         | size  | Large
| 2   | 1         | dist  | Midlands
| 3   | 2         | size  | Medium
| 4   | 3         | dist  | South

The allocation table (allocation)

| key | ticket_key | field | value | count
| 1   | 1          | size  | Large | 10
| 2   | 1          | size  | Medium| 5

I've managed to get the allocations working using the code:

SELECT store_key, quantity FROM 
allocation

INNER JOIN store_metadata
  ON allocation.`field` = store_metadata.`field`
    AND allocation.`value` = store_metadata.`value`

This returns a list of the stores and how many items they should recieve, what I now need to do it order the stores by the distribution attribute.

Any help would be greatly appreciated.


The question isn't asked very well.

To perform ordering by any column in your result set add ORDER BY [column] to the end of the query. E.g.

SELECT store_key, quantity FROM 
allocation

INNER JOIN store_metadata
  ON allocation.`field` = store_metadata.`field`
    AND allocation.`value` = store_metadata.`value`
    ORDER BY allocation.`field`;
0

精彩评论

暂无评论...
验证码 换一张
取 消