tags:

views:

55

answers:

5

This query works:

SELECT Article.id, 
       Article.post_time, 
       Article.post_locked, 
       Article.comments_locked, Article.title,  
       IF(CHAR_LENGTH(Article.content)>2000, 
          RPAD(LEFT(Article.content,2000),2003,'.'), 
          Article.content) as content, 
       Article.tags, Category.*, 
       User.id, User.user_name, 
       Comment.comment_count 
  FROM `articles` as `Article` 
LEFT JOIN `categories` as `Category` ON `Article`.`category_id` = `Category`.`id` 
LEFT JOIN `users` as `User` ON `Article`.`user_id` = `User`.`id` 
LEFT OUTER JOIN (SELECT article_id, count(*) comment_count FROM `comments`) as `Comment` ON `Article`.id = `Comment`.article_id 
    WHERE '1'='1' 
 ORDER BY `Article`.`id` DESC

But when I loop through the resultset to assign the table name along with the field using 'mysql_field_table', the 'content' returns a table name of nothing, while all others have their correct table:

Array ( 
    [0] => Article 
    [1] => Article 
    [2] => Article 
    [3] => Article 
    [4] => Article 
    [5] => 
    [6] => Article 
    [7] => Category 
    [8] => Category 
    [9] => User 
    [10] => User 
    [11] => Comment )

using

for ($i = 0; $i < $numOfFields; ++$i) {
   array_push($table,mysql_field_table($this->_result, $i));
   array_push($field,mysql_field_name($this->_result, $i));
}

Anyone ever try to do this? Have a solution? I want to return less data from my DB in my query. Or is it less intensive (on mysql, memory, cpu) to simply select all content and truncate the content via PHP? I thought returning less from DB would be better.

Thanks a bunch!!

Peace.

EDIT

to clear up, this is the result, you will see why it isnt what I want:

Array ( 
    [0] => Array ( 
        [Article] => Array ( 
            [id] => 8 
            [post_time] => 1278606312 
            [post_locked] => 0 
            [comments_locked] => 0 
            [title] => Article 8
            [tags] => test ) 
        [] => Array ( 
            [content] => my content for Article  ) 
        [Category] => Array ( 
            [id] => 2 
            [name] => cat2 ) 
        [User] => Array ( 
            [id] => 3 
            [user_name] => user3 ) 
        [Comment] => Array ( 
            [comment_count] => 1 ) 
    ) 
   [1] => Array ( 
        [Article] => Array ( 
            [id] => 7 
etc...
+1  A: 

no you cant use a as [tablename].[columnname]-like format for custom column names.

It would be weird anyway if it would work, because how can content be defined as 'Article.content' if it's not really part of the Article table dataset.

Just select the columns you need and join where needed.

But what's WHERE '1' = '1' doing in there? that will just evaluate to true as it is a boolean expression, but it won't affect your resultset.

Laurens Ruijtenberg
Yes the where is useless, thanks.
KrNel
i.e, its useless but its there for when there is no additional conditions added, otherwise the way its setup, the query gets ' WHERE ' with no condition and the query fails. I should edit my query construction to make it better, but irrelevant to the problem/solution to the tablename for calculated resultset. Thanks.
KrNel
+1  A: 

In order to use characters beyond the English alphabet and spaces in a column alias, the standard SQL means requires using double quotes (though MySQL supports using backticks IE: "`" too):

...,
IF(CHAR_LENGTH(Article.content)>2000, 
   RPAD(LEFT(Article.content,2000),2003,'.'), 
   Article.content) AS "Article.content", 
...
OMG Ponies
Thanks, I had tried ` ` backticks, forgot it wasn't what I wanted. Sorry about mentioning 'as table.column' as another option I wanted. Forget about that :) The mysql_field_table is where I am sad. Can't get the table on a calculation/condition.Thanks.
KrNel
+1  A: 

But when I loop through the resultset to assign the table name along with the field using 'mysql_field_table', the 'content' returns a table name of nothing, while all others have their correct table

Once you've done that magic on Article.content, to create the content field, it no longer belongs to the Article table. Rather, it belongs to the result set of that query. I believe that's the explanation for having no table associated with that field.

Imagine a GROUP BY query, with something like COUNT(*) as number. 'number' doesn't belong to any table.

George Marian
Thanks, but as I saw when I don't put an as, I get a column name of the condition: 'IF(CHAR_LENGTH(Article.content)>2000,RPAD(LEFT(Article.content,2000),2003,'.'),Article.content)', so its no good. The resulting array groups the columns with common table together, but with the content, its put it into another array [] empty that cannot be accessed using a key. If the mysql_field_table would work, then the new array of results built on mysql_field_table and mysql_field_name would have all the Article fields in one array.
KrNel
@KrNel When you don't use `as content` does it show up with a table name in that array?
George Marian
No, its still []. Please see my edit above, I added the output which I should have done in the first place to facilitate visually of th eproblem with the table name not being returned on the calc/cond.
KrNel
@KrNel Simply put, you'll have to find a way around this issue. I suggest fusing with the string outside of the database, assuming that having the table name is the higher priority.
George Marian
Yup already done, didnt want to cheat. Thanks.
KrNel
@KrNel It's not cheating. It's just dealing with the design of the tool. It makes sense that the calculated field isn't being associated with any table. Technically, it's not part of the table, it's part of the result set.
George Marian
Hehe, yeah I guess theres no cheating in programming, but I wanted to get it to work via mysql commands without modding the process of creating the array. Thanks.
KrNel
A: 

If you really need the ability to know that the column had a particular source, could you have a view on top of Article which does this manipulation to content? Then the source would appear to be the view? Unfortunately, MySQL doesn't support declared computed columns in tables, that might also be useful to you in this case.

Cade Roux
quote: "MySQL doesn't support declared computed columns in tables, that might also be useful to you in this case."Indded, that appears to be the case as I suspected, but I wanted to ask the pros here and see if it was possible ;) Thanks.
KrNel
A: 
while ($row = mysql_fetch_row($this->_result)) {
    $prev_table;
    for ($i = 0;$i < $numOfFields; ++$i) {
        if ($table[$i] == "") {
            $tempResults[$prev_table][$field[$i]] = $row[$i];
        }else {
            $tempResults[$table[$i]][$field[$i]] = $row[$i];    
        }
        $prev_table = $table[$i];
    }
}

Oh well, mysql couldnt do what I wanted. I added the prev_table to take the one before ;)

Thanks to everyone for the help.

KrNel