大家好,
我试图弄清楚如何把这一点放到文字中,但我想知道如何在“多表”mysql查询中分别格式化每个表的输出。墙“table1”的输出在while循环中被格式化,但是来自table2 " actions“的内容在被插入到表(列action_body)之前已经格式化(作为带有链接的一行文本),所以在循环中我将只输出来自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
}感谢你的帮助,谢谢,莉亚
发布于 2010-01-10 17:14:24
注释后的更新
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发布于 2010-01-10 17:44:03
因为您没有连接特定的字段,所以您将获得两个表的每个行-行组合,这可能比您想要的数据要多得多。
最好是执行两个查询,每个表一个。在循环遍历每个表的结果时,您可以将所需的数据收集到一个数组中,并使用要对其进行排序的字段作为数组键。
然后对数组进行排序,并通过循环将其打印出来。
https://stackoverflow.com/questions/2036435
复制相似问题