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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:33:27+00:00 2026-05-28T16:33:27+00:00

I have been on this mysql query for 2 days running. This is the

  • 0

I have been on this mysql query for 2 days running. This is the scenario:

I have two tables: users and accounts. There structures below:

$sql="create table if not exists users (
    id bigint(20) not null auto_increment,
    username varchar(255) not null,
    password varchar(255) not null,
    email varchar(255),
    phone varchar(40),
    PRIMARY KEY (id, username))";

$sql="create table if not exists accounts (
    id int not null auto_increment, primary key(id),
    userid int(11) not null,
    type varchar(20) not null, <----- we have two types: bill and pay ------>
    amount varchar(255) not null,
    date_paid datetime not null)";

What I Want To Do:

I want to select the phone numbers of people owing over 10,000 and paid or billed last 14days ago or more.

How do we find people that are owing:

When are you billed (assuming 50, 000) a row is added in the db like this:

insert into accounts (id, userid, type, amount, date_paid) values ('', 'id of the person', 'OWE', 50000, '$date');

When the person makes a pay (assume 20, 000), a row is also inserted:

insert into accounts (id, userid, type, amount, date_paid) values ('', 'id of the person', 'PAY', 20000, '$date');

To get the amount the person is owing:

ALL BILLS – ALL PAYMENTS

What i came up with:

select phone  from users u, accounts a1, accounts a2 where u.id=a1.userid and phone != '' and (((select sum(a1.amount) from a1 where a1.userid=u.id and a1.type='owe') - (select sum(a2.amount) from a2 where a2.userid=u.id and a2.type='pay')) >= 10000) and datediff(NOW(), select max (a1.datetime) as datetime from a1 where a1.userid=u.id) > 14 group by u.id

I have been modifying this query for a long time and the best i get are errors. Some of which are:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near select max (a1.datetime) as datetime from a1 where a1.userid=u.id) > 14 group by

When i remove the last and clause it shows: table db.a2 does not exist.

Please how can i go about this?

  • 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-28T16:33:28+00:00Added an answer on May 28, 2026 at 4:33 pm

    Hints:

    • it would be better when amount would be an decimal or something
    • store the payments with negative amount and you can simply sum over amount to get the balance

    get the balance

    SELECT u.id, SUM(CASE WHEN type = 'OWE' 
                     THEN CAST(amount AS DECIMAL(10,2)) 
                     ELSE CAST(amount AS DECIMAL(10,2))*-1 END ) as balance
    FROM users u
    INNER JOIN accounts a ON u.id = a.userid
    GROUP BY u.id 
    HAVING balance > 10000
    

    check that the phone number has at least 5 character

    WHERE CHAR_LENGTH(phone) > 4  
    

    All together + Date

    SELECT u.phone, u.id, SUM(CASE WHEN type = 'OWE' 
                     THEN CAST(amount AS DECIMAL(10,2)) 
                     ELSE CAST(amount AS DECIMAL(10,2))*-1 END ) as balance,
                 MAX (date_paid) as last_action
    FROM users u
    INNER JOIN accounts a ON u.id = a.userid
    WHERE CHAR_LENGTH(phone) > 4 
    GROUP BY u.id 
    HAVING balance > 10000 OR DATE_SUB(CURDATE(),INTERVAL 14 DAY) <= last_action
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi For many days I have been working on this problem in MySQL, however
I've been struggling with this query for a few days now. I'm using PHP/MySQL.
I have been banging my head on this (im a newbie in MySQL, be
I am using some MySQL tables. Their structures are listed below. login: loginid username
sorry to post this but have been struggling now for 8 days and really
I have been searching round the internet for days on this matter but I
Ok I have been stuck on my website for like 2 days with this?
I've been trying to get this complex MYSQL query to work exactly right over
I have two arrays in two MYSQL tables. I want to loop through the
I've been working on this for two days, can't get it, I need some

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.