全部,
我试图弄清楚如何将它写成单词,但我想知道如何在“多表”mysql查询中分别格式化每个表的输出。 table1“wall”的输出在while循环中格式化,但table2“actions”中的内容在插入表(column_body)列之前已经格式化(带有链接的1行文本),所以在循环我只会从actions表中输出action_date和action_body列。
我可能没有使用正确的sql方法(如果我正在做任何事情,那就是)我需要的结果,所以请随意纠正我的新手示例,或建议一种新方法来解决这个问题。
查询:
$query = "SELECT wall.wall_id, wall.wall_owner_id, wall.wall_user_id,
wall.wall_post_date, wall.wall_post_content, actions.action_id,
actions.action_date, actions.action_user_id, actions.action_title,
actions.action_body FROM wall, actions
ORDER BY wall.wall_post_date, actions.action_date DESC";
$result = mysql_query($query);
while( $rows = mysql_fetch_assoc($result) {
// What to put here
}
感谢任何帮助,谢谢,Lea
答案 0 :(得分:2)
评论后更新
SELECT w.* FROM (
(SELECT
'w' as type,
wall_id as id,
wall_owner_id as owner_id,
wall_user_id as user_id,
wall_post_date as post_date,
NULL as title,
wall_post_content as content
FROM wall
WHERE wall_owner_id = x # user id of owner
)
UNION
(SELECT
'a' as type,
action_id as id,
action_user_id as owner_id,
NULL as user_id,
action_post_date as post_date,
action_title as title,
action_body as content
FROM actions
WHERE action_user_id = x # user id of owner
)
) w
ORDER BY w.post_date DESC
答案 1 :(得分:1)
因为你没有加入特定的字段,所以你将得到两个表的每个行 - 行组合,这比你想要的数据要多得多。
通过执行2个查询,每个表一个查询,您会更好。循环遍历每个表的结果时,您可以在一个数组中收集所需的数据,并使用要对其进行排序的字段作为数组键。
然后你对数组进行排序,然后遍历它以将其打印出来。