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 7599699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:43:37+00:00 2026-05-30T22:43:37+00:00

Here’s the table that I’m working with: CREATE TABLE `index` ( `wid` int(7) unsigned

  • 0

Here’s the table that I’m working with:

CREATE TABLE `index` (
 `wid` int(7) unsigned NOT NULL,
 `pid` int(7) unsigned NOT NULL,
 `value` tinyint(3) unsigned NOT NULL DEFAULT '0',
 UNIQUE KEY `wid` (`wid`,`pid`),
 KEY `pid` (`pid`),
 KEY `value` (`value`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

I have integer values of “pid” matching various “wid” that are only unique together.

For instance, I have:

pid - wid - value
 1  -  5  -  7
 1  -  6  -  5
 1  -  7  -  9
 2  -  5  -  2
 2  -  8  -  4
 3  -  8  -  8
 3  -  7  -  12
 4  -  5  -  5
 .. -  .. -  ..

Now let’s assume I want to get PID where WID matches 5 and 7 order by the highest sum of VALUE.

So PID 1 matches 2 WID’s (5,7) whose sum VALUE is 16 (7 + 9).
And PID 2 matches only 1 WID (5) whose sum VALUE is 2.
And PID 3 matches WID (7) whose sum VALUE is 12.

I need to construct the query that does this and sorts by SUM VALUE descending.

Such as:

SELECT
    index.pid, SUM(index.value) TotalSum
FROM index
WHERE
    index.wid = 5 or index.wid = 7
GROUP BY UNIQUE(index.pid)
ORDER BY TotalSum DESC

I know this query is incorrect but I’m trying to show what I’m trying to accomplish.

  • 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-05-30T22:43:39+00:00Added an answer on May 30, 2026 at 10:43 pm

    What you are trying to accomplish is approx correct.

    Try below :

    SELECT index.pid, SUM(index.value) TotalSum
    FROM index
    WHERE (wid = 5 or wid = 7)   //if you want only from wid 5,7 elase no need for it
    GROUP BY index.pid
    ORDER BY TotalSum DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes
Here's my procedure: DROP PROCEDURE IF EXISTS `couponExpires`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `couponExpires`(IN couponID BIGINT,
Here is my problem : I have a post controller with the action create.
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here's what I'm trying to accomplish with this program: a recursive method that checks

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.