简体   繁体   English

Mysql连接两个表问题

[英]Mysql join two tables issue

I have two tables in my database. 我的数据库中有两个表。 Am struggling to join those tables to get data. 我正在努力加入这些表来获取数据。

My game table look like this way 我的游戏桌看起来像这样

在此输入图像描述

My fb_request table look like this way 我的fb_request表看起来像这样 在此输入图像描述

My aim is to get game information from game table like venue, game_date, logo for user_id = 17. I have set game_selected as foreign key in my fb_requests table. 我的目标是从诸如场地,game_date,user_id = 17的徽标等游戏表中获取游戏信息。我已将game_selected设置为我的fb_requests表中的外键。 Please help me to write a join query for these two tables. 请帮我写这两个表的连接查询。 Thank in advance. 预先感谢。 Now am using separate select query to fetch data 现在我使用单独的选择查询来获取数据

Select g.venue,g.game_date,g.logo,fb.game_selected as game_id,fb.accept_status,fb.request_id 
 from fb_request fb 
 left join game g on (fb.game_selected=g.id)
 where fb.user_id=17;

Try this,please check your table name 试试这个,请检查你的表名

Select tbl1.venue,tbl1.game_date,tbl1.logo tbl_game tbl1
    inner join fb_request tbl2 on tbl1.id = tbl2.id
    where tbl2.user_id = 17
SELECT 
venue, game_date, logo 
FROM game_table JOIN fb_request_table 
ON (id = game_selected) 
WHERE user_id = $user_id 
GROUP BY user_id, game_selected

The GROUP BY makes sure you get unique combination of user_id and game_selected values GROUP BY确保您获得user_idgame_selected值的唯一组合

I suggest you go through Visual Representation of JOINS to get an idea of how JOINS work. 我建议你通过JOINS的Visual Representation来了解JOINS工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM