duandanbeng1829 2014-06-19 10:48
浏览 135

mysql连接两个表,一个表有多个匹配的行

I'm trying to join two tables. lets say first table is test1 and other one is test2 in test1 table I have fields HOTEL_ID NAME CITY COUNTRY

Then in the test2 table I have ROOM_ID HOTEL_ID ROOM_TYPE

so most of the time a hotel is having many type of rooms. so when I join these two tables using this query

$this->db->like('city', $visitingPlace);
$this->db->or_like('country', $visitingPlace);
$this->db->or_like('name', $visitingPlace);
$this->db->limit($per_page, $page);
$this->db->select('*')
         ->from('test1')
         ->join('test2', 'test1.hotel_id = test2.hotel_id', 'left');
$query = $this->db->get();

it is repeatedly showing the hotel name for each room type matching to that hotel id but I want to show hotel name once with all the room types. how can I achieve this?

Thank You

  • 写回答

2条回答 默认 最新

  • dqysi86208 2014-06-19 10:51
    关注

    The question is tagged "mysql", so here is a MySQL solution:

    select h.*, group_concat(distinct room_type) as RoomTypes
    from test1 h left join
         test2 r
         on h.hotel_id = r.hotel_id
    group by h.hotel_id;
    

    For each hotel, this will produce a column with a list of the room types at the hotel.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?