t_msg
msg_id | message | user_id
示例数据:
01 | hai how are you | 01
02 | will you marry me ? | 02
t_users
示例数据:
uid | username | full_name
01 | greg | Greg Fish
02 | drown | Leon Drown
t_friendship
示例数据:
uid | uid_fk | status
01 | 02 | friend
02 | 01 | friend
我有3个表关系。现在我想要来自用户节目的消息和来自其他用户的消息,如果是状态朋友
如果uid 1已登录,请执行以下示例:
uid 1
你好,你好吗
uid 2(这个节目因为uid 1和uid 2是朋友。 你愿意嫁给我吗?
这是我做的查询:
$query = mysql_query("
SELECT
M.msg_id,
M.uid_fk,
M.message,
M.created,
U.full_name,
U.profile_pic,
U.username,
U.uid,
F.status,
F.uid
FROM
t_haps_wall M,
t_users U,
t_join_user F
WHERE
M.uid_fk=U.uid
答案 0 :(得分:1)
你必须JOIN
三个表,ON f.uid_fk = u.uid
和线程表,如下所示:
SELECT
M.msg_id,
M.uid_fk,
M.message,
M.created,
U.full_name,
U.profile_pic,
U.username,
U.uid,
F.status,
F.uid
FROM t_haps_wall M
INNER JOIN t_users U ON m.user_id = u.uid
INNER JOIN t_join_user F ON f.uid_fk = u.uid