dongmeba4877 2015-07-18 15:43
浏览 52
已采纳

sql - 用条件连接两个表! 笨

I really don't know how to explain my problem, so i'm gonna provide an example:

i have two tables:

Packages:
pack_id , pack_name 100 , Kish Island Tour 101 , Ancient Persia Tour 102 , Persian Gulf Tour Package_plan:
plan_id , pack_id , double_bed_price 1 , 100 , 100$ 2 , 100 , 90$ 3 , 100 , 100$ 4 , 101 , 150$ 5 , 101 , 180$ 6 , 102 , 80$ 7 , 102 , 110$

I want to join these two tables On pack_id , and retrieve least value of double_bed_price for each packages !

more illustration , the required answer is:

  package_id , plan_id , double_bed_price
      100    ,    2    ,    90$
      101    ,    4    ,    150$
      102    ,    6    ,    80$

I'm using PHP Codeigniter , and its preferred to code in active record, but there is no problem with simple SQL query.

so many thanks to all

  • 写回答

1条回答 默认 最新

  • douzhenzu0247 2015-07-18 16:31
    关注

    try this:

    select pp.pack_id , pp.plan_id , min(double_bed_price)
    from Packages p join Package_plan pp on p.pack_id = pp.pack_id
    group by p.pack_id
    

    Assuming that the double_bed_price datatype is Numeric Data Type.

    If String Type then remove $ character at the end and convert to numeric data type to get correct min value.

    CONVERT(REPLACE(double_bed_price, '$', ''), UNSIGNED INTEGER)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料