2012-07-18 57 views
0

所以,簡單解釋:我想從表中的所有記錄中按最高傢俱值(pricelist_furnis_tradevalue.value)排序的games_records乘以記錄數量(= games_records.amount)。 furni值在pricelist_furnis_tradevalue中。 現在... pricelist_furnis_tradevalue對於某些傢俱有多個條目。我只想獲得最新的條目,因此應該按pricelist_furnis_tradevalue.time排序。 但是,下面的這個查詢返回每個furni /記錄的pricelist_furnis_tradevalue的所有條目。SQL加入表格並按乘積值排序

SELECT * 
FROM games_records 
JOIN pricelist_furnis_tradevalue 
    ON games_records.furni = pricelist_furnis_tradevalue.furni 
ORDER BY (pricelist_furnis_tradevalue.value*games_records.amount) DESC 

這裏有2級所需的表的結構

games_records

id  user  furni  amount  time 
1  2   4   40   1338052926 
2  4   30   90   1338054046 

pricelist_furnis_tradevalue

id  furni  value  time 
1  2   20   1334065379 
2  2   50   1334067445 
3  2   100   1334092057 
4  4   50   1334065375 
4  4   20   1334067436 

我會很感激任何幫助!謝謝!

+0

你試過選擇'pricelist_furnis_tradevalue.value * games_records.amount'? – Kermit 2012-07-18 19:49:01

+0

@njk並用AS變量定義它們,然後用ORDER BY變量DESC定義它們? **編輯**試過了。沒有工作。 – albin 2012-07-18 19:52:26

回答

0

您需要以最長時間獲取記錄。

這裏有一個方法:

SELECT * 
FROM games_records join 
    (select furni, max(time) as maxtime 
     from pricelist_furnis_tradevalue 
     group by furni 
    ) as maxf 
    on games_records.furni = maxf.furni JOIN 
    pricelist_furnis_tradevalue 
    ON games_records.furni = pricelist_furnis_tradevalue.furni and 
     pricelist_furnis_tradevalue.time = maxf.maxtime 
ORDER BY (pricelist_furnis_tradevalue.value*games_records.amount) DESC 
+0

謝謝!這完美的作品! – albin 2012-07-18 20:31:15

0
SELECT * FROM 
    (SELECT *, pricelist_furnis_tradevalue.value*games_records.amount AS 'mult' FROM games_records 
    JOIN pricelist_furnis_tradevalue 
    ON games_records.furni = pricelist_furnis_tradevalue.furni) a 
ORDER BY a.mult DESC