简体   繁体   English

php mysql UNION ALL-从单独的表获取ID

[英]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. 我正在做一个时间轴,效果很好,但是我正尝试使其变得如此以便它也添加了图片...努力寻找解决方案,我将尽我所能地保持清晰。 UNION ALL在获取和显示我需要的信息方面做得很好,但是,当我尝试从图像表中获取该事件的图像时,该图像表却保留了我被卡住的路径。

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.. 我一直在上网,而Stack Overflow我一直在想我需要做一个左联接,我可能错了,我不确定,但是那是我认为正确的方法。

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 错误日志:PHP警告:mysqli_fetch_array()期望参数1为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 :) 我要收集的是成员的ID,因为论坛部分没有任何图片,但是成员有,在时间轴上,我希望显示成员图片:)

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 . 上面的操作可以使所有记录一次获得,但是在输出数组中将有重复的成员ID,因为一个成员将具有不同的帖子。 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' . 更好的方法是在内部联接中使用“ members”和“ images”表(希望一个用户将拥有一个配置文件图像)并创建第二个sql,以获取来自“ members”的每个成员ID的所有帖子。 Hope the above solution will help you . 希望以上解决方案对您有所帮助。 Please inform me if any error comes though . 如果有任何错误,请通知我。

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

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