简体   繁体   中英

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 在此输入图像描述

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. 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

I suggest you go through Visual Representation of JOINS to get an idea of how JOINS work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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