Php MYSQL连接查询通知

Php MYSQL连接查询通知,php,mysql,Php,Mysql,我有四个表commenttable、posttable、usertable和notifications表。我想建立一个通知系统。通知表包含以下内容 column id, notifier, notifying, date, type, typeid 类型可以是1或2。如果是1,则是评论;如果是2,则是帖子 我想构建一个MySQL查询,它获取用户的所有通知,并根据类型将它们与commenttable、posttable连接起来。例如,如果type为1,type id为300,那么它将从commm

我有四个表commenttable、posttable、usertable和notifications表。我想建立一个通知系统。通知表包含以下内容

column id,
notifier,
notifying,
date,
type,
typeid
类型可以是1或2。如果是1,则是评论;如果是2,则是帖子

我想构建一个MySQL查询,它获取用户的所有通知,并根据类型将它们与commenttable、posttable连接起来。例如,如果type为1,type id为300,那么它将从commmentable中提取comment列;如果type为2,那么它将从post表中提取post列

文章中的专栏如下:

postid, post and commenttable commenter, comment, commentid

我构建了一个查询,如下所示,但它不能像我希望的那样工作

SELECT 
    notificationstable.who,
    notificationstable.type,
    notificationstable.timestamp,
    notificationstable.date,
    commenttable.comment,
    commenttable.commentid,
    usertable.username,
    usertable.avatar,
    usertable.userid,
    usertable.verified,
    posttable.photo,
    posttable.title,
    posttable.postid
from
    notificationstable
        inner join
    usertable
        inner join
    posttable
        inner join
    commenttable ON notificationstable.who = usertable.userid 
 and posttable.postid = notificationstable.type 
 and commenttable.commentid = notificationstable.type
where
    notificationstable.whom = '$userid'
order by notificationstable.date desc

$userid
是一个php变量

考虑到通知似乎不能同时应用于posttable和commenttable,您认为您的意思是进行外部联接吗

像这样的

SELECT 
    notificationstable.who,
    notificationstable.type,
    notificationstable.timestamp,
    notificationstable.date,
    commenttable.comment,
    commenttable.commentid,
    usertable.username,
    usertable.avatar,
    usertable.userid,
    usertable.verified,
    posttable.photo,
    posttable.title,
    posttable.postid
FROM notificationstable
INNER JOIN usertable
ON notificationstable.who = usertable.userid
LEFT OUTER JOIN posttable
ON posttable.postid = notificationstable.type
LEFT OUTER JOIN commenttable 
ON commenttable.commentid = notificationstable.type
WHERE notificationstable.whom = '$userid'
ORDER BY notificationstable.date DESC

如果你想让别人帮助你,那么“它不能像我希望的那样工作”是一个非常糟糕的问题描述。它给了你什么结果?你期望得到什么样的结果。如果您将表格和一些数据放入SQL Fiddle中供我们使用,这将非常有帮助。在问题中没有问号可能暗示这不是问题。
userrable
在我看来首先是一个输入错误!我不喜欢那些没有关于子句“我有三个表commenttable、posttable、usertable和notifications table”的
的内部联接。HeHe从OP的帖子中,我不知道应该是什么,但我认为
posttable.postid=notificationstable.type
是个问题。他说,类型要么是1要么是2,如果是1,则应执行commenttable查询;如果是2,则应执行posttable查询。我认为在commenttable.commentid=notificationstable上的外部连接commenttable.commentid=notificationstable。无论与之对应的列名是什么,comment或post id都是这个想法。我不是MYSQL忍者,但是您可以这样做:
…CASE WHEN notificationstable.type=1,然后在commenttable.commentid=notificationstable.still\u需要此列,或者在posttable.postid=notificationstable.still\u需要此列
并可能对select执行类似的操作吗?我想您可以这样做是正确的,但很难从原来的帖子中辨认出来。我接受了他的查询(没有尝试使用它),并将其修改为使用外部联接。使用内部联接如果通知类型定义了它是一个post表还是一个注释表,那么两者不能同时与post和注释匹配。您可以执行左连接,然后使用CASE(或只是IF)来定义哪些表列作为特定列返回。我考虑使用CASE,使CASE类型为1,那么查询putput中的post列将为null,反之亦然,如果类型为2。理想情况下,我希望输出@kickstart使用左连接,但不匹配的列无论如何都将为NULL