tags:

views:

300

answers:

2

HI all,

I am trying to figure out how to put this into words even, but I am wanting to know how to format the output from each table separately in a "multiple table" mysql query. The output from the table1 "wall" is formatted within a while loop, but the content from table2 "actions" is already formatted(as 1 line of text with links) before it is inserted into the table(column action_body), so inside the loop I would only be outputting the action_date and action_body columns from the actions table.

I am probably not using the correct sql method(if Im doing anything right at all, that is) for the results I need, so feel free to correct my novice example, or suggest a new way to approach this.

Query:

$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
}

Any help is appreciated, thanks, Lea

+2  A: 

Update after comments

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
Tor Valamo
Yes, I guess that query would be right, but how do I handle the actions table data? It contains html in the action_body field.
Lea
Well when you insert it, don't use html...?
Tor Valamo
I want to join them, because they are put into an array that is a list of wall posts, and user actions sorted by their dates... If you are suggesting it would be better to do two queries, how would I put both query results into the same array?
Lea
I am using html in the action_body field because I am unsure how to format(with html) the actions output differently to the wall output, as the actions output is just one line of text, whereas the wall output includes a html table that I use to layout the data.
Lea
So you want to merge them into one list where wall items and action items are in THE SAME list? This isn't possible right now, unless you have two sets of columns that have the same types. wall_user_id has action_user_id and so on. I'd suggest using a separate table. I'll edit my post.
Tor Valamo
Thanks. How would I put both query results into the same array?
Lea
Thanks for your help ++++
Lea
Edited it slightly so you don't need a separate table.
Tor Valamo
Yes, I noticed, thanks so much for your help, I was banging my head to figure out this one:)
Lea
+1  A: 

Because you don't JOIN on a specific field, you're gonna get every row-row combination of the two tables, which is a whole lot more data than you probably want.

You'd be better of by doing 2 queries, one for each table. While looping through the result of each table, you can collect the data you want in one array, with the field you want to sort it by as array key.

Then you sort the array, and loop through it to print it out.

Jimmy Shelter