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

  • Home
  • SEARCH
  • 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 7739881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:38:05+00:00 2026-06-01T08:38:05+00:00

I have 3 tables – user, service and ratings. The user’s primary key is

  • 0

I have 3 tables – user, service and ratings. The user’s primary key is user_id and is a foreign key in the service table. Its also a foreign key in ratings table linked to rated_id (the user id of the person being rated) and rater_id (the user id of the person providing the rating)

Each user has one service and can have mutltiple ratings. If the pushed field in the ratings table is 1, then its a valid rating and can be used. If its 0, its not valid yet and is waiting to be pushed.

I need a query to show me a list of all services, sorted in decending ordered by the users with most ratings. Services belonging to Users with ratings but are not pushed are at the bottom along with services belonging to users with no ratings.

Here is the sql to create the table and data:

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `ratings`
--

-- --------------------------------------------------------

--
-- Table structure for table `ratings`
--

CREATE TABLE IF NOT EXISTS `ratings` (
  `unique_id` int(11) NOT NULL AUTO_INCREMENT,
  `rater_id` int(11) NOT NULL,
  `rated_id` int(11) NOT NULL,
  `pushed` int(11) NOT NULL,
  PRIMARY KEY (`unique_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `ratings`
--

INSERT INTO `ratings` (`unique_id`, `rater_id`, `rated_id`, `pushed`) VALUES
(1, 4, 1, 1),
(2, 4, 1, 1),
(3, 4, 2, 1),
(4, 4, 3, 0);

-- --------------------------------------------------------

--
-- Table structure for table `service`
--

CREATE TABLE IF NOT EXISTS `service` (
  `unique_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `description` varchar(100) NOT NULL,
  PRIMARY KEY (`unique_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `service`
--

INSERT INTO `service` (`unique_id`, `user_id`, `description`) VALUES
(1, 1, 'marks service'),
(2, 2, 'shanes service'),
(3, 3, 'peters service');

-- --------------------------------------------------------

--
-- Table structure for table `user`
--

CREATE TABLE IF NOT EXISTS `user` (
  `user_id` int(11) NOT NULL,
  `name` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`user_id`, `name`) VALUES
(1, 'mark'),
(2, 'shane'),
(3, 'peter'),
(4, 'jobposter');

and here is a query i came up with

SELECT s.*, count(r.rated_id), r.pushed FROM service s

join user u on (u.user_id = s.user_id)
join ratings r on (r.rated_id = u.user_id)

group by r.rated_id

order by r.rated_id

the problem is its incomplete. users with no ratings wont show and if a user has multiple ratings but none are pushed, they will appear high up…

  • 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-01T08:38:07+00:00Added an answer on June 1, 2026 at 8:38 am

    Try this query –

    SELECT
      s.*, r.rated_id, r.pushed_count
    FROM
      service s
    JOIN user u ON
      u.user_id = s.user_id
    LEFT JOIN (
        SELECT
          rated_id, COUNT(IF(pushed = 1, 1, NULL)) pushed_count
        FROM
          ratings
        GROUP BY
          rated_id
      ) r
      ON r.rated_id = u.user_id
    ORDER BY
      r.pushed_count DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables foo and bar: create table foo(a int, b varchar(10), primary key
I have tables linked by FK, I query on the first table using entity
I have three tables A: A.pID primary key, A.Name nvarchar(250) B: B.pID primary key,
I have tables a and b; b references a via foreign key. I copied
I have tables queue and requests. queue's reqID is a foreign key to requests'
I have tables as below. Table Messages message_id parent_id forum_id user_id Table Users user_id
Hi i have tables like this : table entry : id | total_comments _____________________
I have tables: Teachers and Students. We have a SpecialProjects Table where Teachers and
I have tables item and store (it's a store management system). item table has
I have tables & data like this: venues table contains : id +----+---------+ |

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.