views:

75

answers:

3

Hi, I'm not much of a SQL man so I'm seeking help for this one. I have a site where I have a database for all accounts and whatnot, and another for storing actions that the user has done on the site.

Each user has their own table but I want to combine the data of each user group ( all users that are "linked together" ) and order that data in the time the actions took place.

Heres what I have;

<?php
$query = "SELECT `TALKING_TO` FROM `nnn_instant_messaging` WHERE `AUTHOR` = '" . DISPLAY_NAME . "' AND `TALKING_TO` != ''";
$query = mysql_query( $query, $CON ) or die( "_error_ " . mysql_error());
if( mysql_num_rows( $query ) != 0 ) {
    $table_str = "";
    $select_ref_clause = "( ";
    $select_time_stamp_clause = "( ";
    while( $row = mysql_fetch_array( $query ) ) {
        $table_str .= "`actvbiz_networks`.`" . $row['TALKING_TO'] . "`, ";  
        $select_ref_clause .= "`actvbiz_networks`.`" . $row['TALKING_TO'] . ".REF`, ";
        $select_time_stamp_clause .= "`actvbiz_networks`.`" . $row['TALKING_TO'] . ".TIME_STAMP`, ";
    }   
    $table_str = $table_str . "`actvbiz_networks`.`" . DISPLAY_NAME . "`";
    $select_ref_clause = substr($select_ref_clause, 0, -2) . ") AS `REF`, ";
    $select_time_stamp_clause = substr($select_time_stamp_clause, 0, -2) . " ) AS `TIME_STAMP`";    
}else{
    $table_str = "`actvbiz_networks`.`" . DISPLAY_NAME . "`";   
    $select_ref_clause = "`REF`, ";
    $select_time_stamp_clause = "`TIME_STAMP`";
}
$where_clause = $select_ref_clause . $select_time_stamp_clause;

$query = "SELECT " . $where_clause . " FROM " . $table_str . " ORDER BY TIME_STAMP";
die($query);
$query = mysql_query( $query, $CON ) or die( "_error_ " . mysql_error());
if( mysql_num_rows( $query ) != 0 ) {

}else{
?>
<p>Currently no actions have taken place in your network.</p> 
<?php   
}
?>

The code above returns the sql statement:

SELECT ( `actvbiz_networks`.`john_doe.REF`, `actvbiz_networks`.`Emmalene_Jackson.REF`) AS `REF`, ( `actvbiz_networks`.`john_doe.TIME_STAMP`, `actvbiz_networks`.`Emmalene_Jackson.TIME_STAMP` ) AS `TIME_STAMP` FROM `actvbiz_networks`.`john_doe`, `actvbiz_networks`.`Emmalene_Jackson`, `actvbiz_networks`.`act_web_designs` ORDER BY TIME_STAMP

I really am learning on my feet with SQL.

Its not the PHP I have a problem with ( I can quite happly code away with PHP ) I'ts just help with the SQL statement.

Any help much appreciated,

REgards,

Phil

+1  A: 

Didn't take the time to through your code, but from the question it sounds like you want UNION.

SELECT
3 AS a
UNION SELECT
2 AS a
UNION SELECT
1 AS a
ORDER BY a

Gives

a
=
1
2
3

EDIT: So perhaps something like this?

DROP TEMPORARY TABLE IF EXISTS t1;
DROP TEMPORARY TABLE IF EXISTS t2;
DROP TEMPORARY TABLE IF EXISTS tmp;

CREATE TEMPORARY TABLE t1 (
 a INT(11)
);

CREATE TEMPORARY TABLE t2 (
 b INT(11)
);

INSERT INTO t1 VALUES (1), (2), (3);
INSERT INTO t2 VALUES (4), (5), (6);

CREATE TEMPORARY TABLE tmp
SELECT
b AS f
FROM t2
UNION SELECT
a AS f
FROM t1
ORDER BY f;

SELECT * FROM tmp;

returns

f
=
1
2
3
4
5
6
simendsjo
I think you are on the right lines but the output is not what I am expecting. imagine 2 tables, exactly the same stucture just different table name and data. I want to temporally put all data into one table as such, if you know what i mean.
Phil Jackson
A: 

Have you tried using a view? This way, you can use the UNION as simendsjo suggested and everything is in a single table. This is not exactly a temporary solution but a very nice, clean and efficient way of doing this.

DrColossos
A: 

I endeed up solving by using simendsjo's tips and used UNION ALL

<?php
$query = "SELECT `TALKING_TO` FROM `nnn_instant_messaging` WHERE `AUTHOR` = '" . DISPLAY_NAME . "' AND `TALKING_TO` != ''";
$query = mysql_query( $query, $CON ) or die( "_error_ " . mysql_error());
if( mysql_num_rows( $query ) != 0 ) {
    while( $row = mysql_fetch_array( $query ) ) {
        $table_str = 'SELECT REF, ACTION, TIME_STAMP, DATA, IMAGE, DISPLAY_NAME FROM actvbiz_networks.' . $row['TALKING_TO'] . ' UNION ALL ';   
    }
}else{
    $table_str = "";
}
$table_str = $table_str . 'SELECT REF, ACTION, TIME_STAMP, DATA, IMAGE, DISPLAY_NAME FROM actvbiz_networks.' . DISPLAY_NAME . ' ORDER BY TIME_STAMP DESC';

$query = mysql_query( $table_str, $CON ) or die( "_error_ " . mysql_error());
if( mysql_num_rows( $query ) != 0 ) {
    while( $rows = mysql_fetch_array( $query ) ) {
Phil Jackson
Yeah, sorry. I forgot to mention ALL
simendsjo