SQL连接表并按乘法值对它们进行排序

时间:2012-07-18 19:46:08

标签: mysql sql

所以,简短说明:我想从最高家具价值(pricelist_furnis_tradevalue.value)排序的表games_records中获取所有记录乘以记录金额(= games_records.amount)。 furni值在pricelist_furnis_tradevalue中。 现在...... pricelist_furnis_tradevalue有一些家具的条目。我想获得最新的条目,因此它应该由pricelist_furnis_tradevalue.time订购。 但是下面的查询会返回pricelist_furnis_tradevalue中每个家具/记录的所有条目。

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

我很感激任何帮助!谢谢!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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