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

时间:2018-06-24 15:20:54

标签: php mysql

想知道是否有什么可以澄清一下,或者请帮忙。

我正在做一个时间轴,效果很好,但是我正在尝试使它也添加图片... 竭尽全力寻找解决方案,我将尽我所能来澄清... UNION ALL可以很好地获取我需要的信息并显示出来,但是,当我试图从图像表中获取该事件的图像时,该图像表保存了我被卡住的路径。

表格示例(非实际表格):

论坛

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

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

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

会员

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

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

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

图片

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

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

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

好的,因此如下面的代码所示,信息显示没有问题:)

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

我一直在上网,而Stack Overflow我一直在想我需要做一个LEFT JOIN,也许我错了,我不确定,但这就是我认为正确的方法。 >

当我执行下面的LEFT JOIN时,我没有页面,只是空白。 错误日志: 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

我要收集的是成员的ID,因为论坛部分没有任何图片,但是成员有,在时间轴上,我想显示成员图片:)

我希望我已经解释了我要实现的目标,并希望如果我错了,有人可以指出正确的方向:) 谢谢!

1 个答案:

答案 0 :(得分:0)

使用此解决方案:-

$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" ;

上面的操作可以让您一次获得所有记录,但是在输出数组中将有重复的成员ID,因为一个成员将具有不同的职位。更好的方法是在内部联接中使用“ members”和“ images”表(希望一个用户将拥有一个配置文件图像)并创建第二个sql,以获取来自“ members”的每个成员ID的所有帖子。希望以上解决方案对您有所帮助。如果有任何错误,请通知我。