tags:

views:

82

answers:

4

Hello, I have the SQL query:

$query = "SELECT yearoffiling, COUNT( id )
FROM files
GROUP BY yearoffiling
LIMIT 0 , 30";

How do I put the values from COUNT(id) inside a string?

Thank you.

+4  A: 
$query = "SELECT yearoffiling, COUNT( id ) total FROM files GROUP BY yearoffiling LIMIT 0 , 30";

$result = mysql_query($query);

while ($ary = mysql_fetch_assoc($result)){

    echo $ary['total'];

}

This is how I would do it "old school". I recommend using prepared statements and PDO these days. Hope this helps.

Jeremy Morgan
Thank you for your reply!
SQL student
I would add the AS attribute to the COUNT(id) part of the query just to ensure mysql labels the column.
Zyris Development Team
AS is not required with mysql, but probably good practice, as it keeps the query similar to other databases where it's required.
Jeremy Morgan
+2  A: 

You can use either the convert() or cast() functions to do this.

SELECT cast(COUNT(id) as char), ....
Vincent Ramdhanie
Thank you for your reply
SQL student
@OMG Ponies...Thanks. Duh, I am attempting to write SQL in the middle of a Java session. Happens all the time. Once I coded all day in VB then tried to deliver a lecture in Java in the night. Only when a student stopped me to ask what I was writing did I realize that all my comments were '.
Vincent Ramdhanie
@Vincent: Happens to all of us
OMG Ponies
This doesn't actually answer the question that the OP intended to ask. He's almost certainly wondering how to get the value of the "count column" into a PHP variable. He has no interest in the internal mysql datatype in play.
Frank Farmer
+2  A: 

Jeremy, just to add to yours i think you may need the AS attribute to do the modifaction

$query = "SELECT yearoffiling, COUNT( id ) AS total FROM files GROUP BY yearoffiling LIMIT 0 , 30";
Zyris Development Team
the AS attribute works, thanks
SQL student
Please select one of the answers on this page that you consider to be the best solution using the check mark beside said answer.
Zyris Development Team
+2  A: 

There's two steps to this:

  1. Define a column alias for the COUNT(id)
  2. Optional: Change datatype MySQL uses for the column to minimize datatype conversion issues

Defining a Column Alias

A column alias is always defined after the column declaration, including computed columns like COUNT(id) in your example. As you've noticed, they're required for computed columns in order to reference the value in other places. You can use the AS notation, or simply declare the alias within single quotes. Examples:

COUNT(id) AS count_id
COUNT(id) 'count_id'

Both are valid syntax, but mind that if you use MySQL keywords you'll have to use backticks (`) to escape the value for the query to execute.

You can name an alias anything you like, but it really should be informational. If unsure, use the hallway test - randomly ask people in the hallway if it makes sense.

Changing the Datatype

This is required when you deal with any programming that does not perform implicit type casting, which would require you to get the column value (in this case an integer) and convert it to a string (or any other desired data type - decimal, float, etc.). You can use either of the MySQL CAST or CONVERT functions - they are synonyms of one another. Examples:

CAST(COUNT(id) AS VARCHAR(4))
CONVERT(COUNT(id), VARCHAR(4))

Be aware that this is occurring with the realm of MySQL, so you can only CAST/CONVERT to MySQL datatypes.

OMG Ponies