Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7780365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:52:23+00:00 2026-06-01T18:52:23+00:00

So I have this sql code in Drupal: $query = db_query(‘SELECT wpv.nid, n.title, AVG(vote)

  • 0

So I have this sql code in Drupal:

$query = db_query('SELECT wpv.nid, n.title, AVG(vote) average FROM {wp_votes} wpv
    LEFT JOIN {node} n on wpv.nid = n.nid
    GROUP BY wpv.nid
    ORDER BY average DESC
    LIMIT 3');

It works just fine, but I need to write it with db_select:

$query2 = db_select('wp_votes','wpv');
$query2->join('node','n','wpv.nid = n.nid');
$query2->fields('wpv',array('nid','vote'));
$query2->fields('n',array('title'));
$rez = $query2->execute()->fetchAll();

My problem is that I don’t know how I can get the average vote(votes are 1,2 or 3).
How do i rewrite the part with AVG(vote). I tried with addEXpression(‘AVG(vote)’) but it didn’t work as expected, I got the average for all the entries, not just for those with the same id.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:52:25+00:00Added an answer on June 1, 2026 at 6:52 pm

    For AVG you need to use addExpression() and groupBy and you will have something like this

    <?php
    $query2 = db_select('wp_votes','wpv');
    $query2->join('node','n','wpv.nid = n.nid');
    $query2->fields('wpv',array('nid','vote'));
    $query2->fields('n',array('title'));
    $alias = $query2->addExpression('AVG(vote)', 'average');
    $query2->groupBy('wpv.nid');
    $query2->sortBy($alias, 'DESC');
    $rez = $query2->execute()->fetchAll();
    

    I’m not sure about sorting by $alias, but theoretical, it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this sql query: SELECT S.SEARCH, S.STATUS, C.TITLE AS CategoryName, E.SEARCH_ENGINES AS Engine,
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I have this SQL query select case when AllowanceId is null then 2 else
I have this sql query Dim cmd As OleDbCommand = New OleDbCommand(SELECT * FROM
I have this JDBC SQL query: select * from table where TX_DATE = {d
i have the following union query sql in my code (SELECT TableA.ID, TableB.Group, ''
i have this SQL code : $sql=SELECT * FROM functions WHERE user_name='ben ' AND
I have this code in Linq. Anybody can provide the t-sql. thanks! var tsr
I always have this notion that writing SQL queries in the code behind is
I'm not good at t-sql, so I need help. I have this code I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.