dpvmtdu364462 2015-11-05 09:42
浏览 5

多表和mm表的mysql语句

i need some help for a mysl-statement. i have a table for "hotels" and a table for "pictogram". both are relatate with mm relation in table hotels_pictogram_mm. on the website the users can choose several pictogram in a search box. so i want to get all hotels which have for example "wellness" AND "free wifi". how must the sql statement be for this search.

following of course does not work:

SELECT *
FROM hotel 
LEFT JOIN hotel_pictogram_mm ON hotel.uid=hotel_pictogram_mm.uid_local 
LEFT JOIN pictogram ON hotel_pictogram_mm.uid_foreign=pictogram.uid 
WHERE 
pictogram.id = 3
AND pictogram.id = 5

thanks for help! martin

  • 写回答

3条回答 默认 最新

  • dongwen1871 2015-11-05 09:48
    关注

    You need to join against pictogram twice, once for each id:-

    SELECT *
    FROM hotel 
    INNER JOIN hotel_pictogram_mm ON hotel.uid=hotel_pictogram_mm.uid_local 
    INNER JOIN pictogram p3 ON hotel_pictogram_mm.uid_foreign=p3.uid 
    INNER JOIN pictogram p5 ON hotel_pictogram_mm.uid_foreign=p5.uid 
    WHERE p3.id = 3
    AND p5.id = 5
    

    You could join once and use count to check the number of matching ids from pictogram, but depends whether you want columns other than those in the hotel table.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分