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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:38:10+00:00 2026-06-04T10:38:10+00:00

I have a table called users where the employee data are stored. Also I

  • 0

I have a table called users where the employee data are stored.
Also I have another table payment_details where employee’s payment related data are stored
the two tables are as follows.

this table is users

sr_no  emp_no  username  payment

1     1001      leroy    <null>
2     1003      harry    <null>
3     1004      Tom      <null>   
4     1008      Jon      <null>    

This table below is payment_details

    sr_no       name     number      month    status        date
      43        Jon       1008       January  paid         5/16/2012
      44        Jon       1008       January  balance      5/16/2012
      45        Harry     1003       January  paid         5/16/2012
      46        Tom       1004       January  paid         5/16/2012
      47        leroy     1001       January  paid         5/16/2012
      48        Jon       1008       January  paid         5/16/2012
      49        Harry     1003       January  paid         5/16/2012
      50        Jon       1008       February balance      5/16/2012
      51        leroy     1001       February paid         5/16/2012
      52        Jon       1008       February paid         5/16/2012
      53        Tom       1004       February balance      5/16/2012

My question here is to update “users” table payment column to “paid” when the status of his/her is all paid in payment_details table

  • 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-04T10:38:13+00:00Added an answer on June 4, 2026 at 10:38 am

    You can either do this: http://www.sqlfiddle.com/#!3/db13f/18

    update users set payment = 'paid'
    from
    (
      select number
      from payment_details
      group by number
      having sum(case when status = 'paid' then 1 end) 
           = count(*)
    ) as x
    where x.number = users.emp_no;
    

    Or this: http://www.sqlfiddle.com/#!3/db13f/19

    update users
      set payment = x.upd
    from 
    (
      select u.emp_no, 
    
          case when sum(case when d.status = 'paid' then 1 end)  = count(*) then
             'paid'
          else
             null
          end as upd
    
      from users u
    
      left join payment_details d
      on d.number = u.emp_no
    
      group by u.emp_no
    ) as x
    where x.emp_no = users.emp_no;
    

    Their difference is how many rows it updates. On the second query, it updates all users, regardless if the user has a paid all status('paid') or not(null); on the first query, it updates only those who are paid.

    The advantage of the second query, is when you change one of the all 'paid' status on payment_detail of a given user to ‘not paid’ for example, it can revert back the user’s payment status to null

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

Sidebar

Related Questions

I have a database table called users This table has two columns (that are
I have two tables called lawyer & client.And they are the users of my
I have a table called users and another called tickets The users table has
I have names stored in my mysql database i created a table called users
I have a table called Users ( class User < ActiveRecord::Base ) and a
Say I have a table called Users, which contains your typical information: Id, Name,
I have a MySQL table called Users. The rows have 4 fields and look
I have a table called UserPermissions with a FK to the users table by
I have a table, called 'files', where I store file_id's. Basically, users on my
I have a table called users, with a field full_name. most of the entries

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.