开发者

drupal 7 sql syntax

i'm trying to get a results set that displays 2 columns from 2 different tables, grouping by one column and summing the other.

this sql works in phpmyadmin:

SELECT SUM(field_count_value), field_region_value
FROM field_data_field_count, field_data_field_region
WHERE field_data_field_count.entity_id = field_data_field_region.entity_id
GROUP BY field_data_field_region.field_region_value
ORDER BY field_count_value DESC

displaying roughly what i need::

-----------------
COUNT    REGION
-----------------
4000     S
600      E
-----------------

but when i convert it to开发者_如何学C drupal 7 sql query syntax:

$nodes = db_query("SELECT SUM(field_count_value), field_region_value
FROM field_data_field_count, field_data_field_region
WHERE field_data_field_count.entity_id = field_data_field_region.entity_id
GROUP BY field_data_field_region.field_region_value
ORDER BY field_count_value DESC");

foreach ($nodes as $record) {
  echo $record->field_region_value . $record->field_count_value; 
  }

i can only get the group by column to display:

-----------------
REGION
-----------------
S
E
-----------------

any help would be greatly appreciated. i've consulted the official d7 docs to get this far -- not very many examples to work from there.


You need to do

$nodes = db_query("SELECT SUM(field_count_value) AS field_count_value, field_region_value
FROM field_data_field_count, field_data_field_region
WHERE field_data_field_count.entity_id = field_data_field_region.entity_id
GROUP BY field_data_field_region.field_region_value
ORDER BY field_count_value DESC");

Otherwise the first column is called "SUM(field_count_value)"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜