简体   繁体   中英

php mysql UNION ALL - get id from seperate tables

wondering if any can shed some light or maybe help please.

I'm doing a timeline, which is working fine, however i'm trying to make it so it adds pictures too... Struggling abit to find the solution, i'll try to be as clear as i can... The UNION ALL is working great getting the information i need and displaying, however as i'm trying to get the images for said event from an image table which holds the path i'm getting abit stuck.

Tables example (Not actual tables):

Forum

|id.....|memberid.|Descrip....|date....|

|71.....|1........|Hello all..|date....|

|82.....|5........|Hows you...|date....|

Members

|id.....|memid....|username...|date....|

|1......|1........|Steve......|date....|

|2......|5........|Donna......|date....|

Images

|id.....|memid....|image     

|47.....|1........|images/stevepic.png|

|139....|5........|images/donnapic.png|

OK, so as the code is below, information displayed great no issues :)

SELECT id as timeline_id, date_time as timeline_date, thread_title as 
timeline_title, post_body as timeline_body FROM forum_posts
UNION ALL
SELECT memid as timeline_id, lastlogin as timeline_date, memusername as 
timeline_title, town as timeline_body FROM members

I've been going through the net, and Stack Overflow and i was thinking i need to do a LEFT JOIN, i maybe wrong i'm not sure but thats what i thought was the right way to go..

When i do the LEFT JOIN below, i get no page, just blank. Error log: PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result

SELECT id as timeline_id, date_time as timeline_date, thread_title as 
timeline_title, post_body as timeline_body FROM forum_posts
UNION ALL
SELECT memid as timeline_id, lastlogin as timeline_date, memusername as 
timeline_title, town as timeline_body FROM members LEFT JOIN membersimages ON membersimages.memid = members.memid WHERE memid = %s

What i'm trying to gather is the ID of the member, as the forum part doesn't have any pictures, but the member does, on the timeline i wish to show member pictures :)

I hope i've explained what i'm trying to achieve and hope someone can point me in the right direction if i've got this wrong :) Thank you!

Use this solution :-

$sql = "select *
        from members as 'mem'
        inner join forum_posts as 'fp'
        on fp.memberid = mem.memid
        inner join images as 'img'
        on img.memid = mem.memid
        where 1" ;

The above you can do to get all records in one shot but there will be repeated member ids in the output array because one member will have different posts . So better will be to use 'members' and 'images' table in inner join (hoping that one user will have one profile image) and create a second sql to get all posts foreach of the member id from 'members' . Hope the above solution will help you . Please inform me if any error comes though .

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