0

我有以下三个表,我想通过 从中提取数据userid,这是赋予它们关系的一个关键字段。我还想按timestamp下面的表模式示例以及我对不起作用的联合查询的示例来按顺序提取数据。

Table1: userid, event_id, event_name, timestamp
Table2: id_user, comment, timestamp
Table3: user_id, photo, timestamp

 $result5 = mysql_query 
     ("(SELECT event_name, timestamp FROM table1 WHERE userid = '25')
    UNION ALL 
       (SELECT comment,timestamp FROM table2 WHERE id_user = '25')
    UNION ALL 
         (SELECT photo,timestamp FROM table3 WHERE user_id ='25')
    ORDER BY timestamp") 
    or die(mysql_error());
4

1 回答 1

2

考虑到并且不相关event_name,您不应该使用任何类型的 . 这些表的共同点是. 您正在寻找一个. 像这样的东西:commentphotoUNIONuser_idJOIN

SELECT a.event_name, a.timestamp, b.comment, b.timestamp, c.photo, c.timestamp
FROM table1 a
INNER JOIN table2 b ON b.id_user = a.userid
INNER JOIN table3 c ON c.user_id = a.userid
WHERE a.userid = '25'
于 2013-01-19T19:40:28.163 回答