Mysql选择第一个和第二个查询

时间:2014-11-02 15:49:22

标签: mysql selection

我有一张表

名 订单号 价钱 ID

记录即。

 name = test
    order_number=1
    price=3
    id=555

    name=test2
    order_number=2 
price=2
    id=555

    name = test3
    order_number=1
    price=5
    id=556

    name = test4
    order_number=2
    price=12
    id=556

有多个输入具有相同的id,order_number最多为10。我需要编写一个查询来比较order_number = 1和order_number = 2的相同id并返回它们的同情心。订单号1的IE价格/具有相同ID的订单号2的价格。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

作为注释,您也可以使用条件聚合而不是join

来执行此操作
select id, (case when order_number = 1 then price end) / (case when order_number = 2 then price end)
from table t
group by id;

答案 1 :(得分:0)

   SELECT t1.price / t2.price
   FROM your_table t1, your_table t2
   WHERE t1.id = t2.id
   AND t1.order_number = 1
   AND t2.order_number = 2

答案 2 :(得分:0)

试试吧 从test11 t1,test11 t2中选择t1.price / t2.price,其中t1.id = t2.id和t1.order_number = 1,t2.order_number = 2;