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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:08:14+00:00 2026-05-23T21:08:14+00:00

I have these tables : mysql> desc mod_asterisk_booking; +—————+——————+——+—–+———+—————-+ | Field | Type |

  • 0

I have these tables :

mysql> desc mod_asterisk_booking;
+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment | 
| uid           | int(10) unsigned | NO   |     | NULL    |                | 
| server_id     | int(10) unsigned | NO   |     | NULL    |                | 
| date_call     | datetime         | NO   |     | NULL    |                | 
| participants  | int(10) unsigned | NO   |     | NULL    |                | 
| ...                                                                      |
+---------------+------------------+------+-----+---------+----------------+

mysql> desc mod_asterisk_servers;
+-------------------+------------------+------+-----+---------+----------------+
| Field             | Type             | Null | Key | Default | Extra          |
+-------------------+------------------+------+-----+---------+----------------+
| id                | int(10) unsigned | NO   | PRI | NULL    | auto_increment | 
| name              | varchar(32)      | NO   |     | NULL    |                | 
| channels_capacity | int(10) unsigned | NO   |     | NULL    |                | 
| ...                                                                          |
+-------------------+------------------+------+-----+---------+----------------+

mysql> desc mod_asterisk_server_phones;
+------------------+------------------+------+-----+---------+----------------+
| Field            | Type             | Null | Key | Default | Extra          |
+------------------+------------------+------+-----+---------+----------------+
| id               | int(10) unsigned | NO   | PRI | NULL    | auto_increment | 
| server_id        | int(10) unsigned | NO   |     | NULL    |                | 
| phone_number     | varchar(15)      | NO   |     | NULL    |                | 
| phone_alias      | varchar(15)      | NO   |     | NULL    |                | 
| extension        | int(10) unsigned | NO   |     | NULL    |                | 
| is_toll_free     | tinyint(1)       | NO   |     | 0       |                | 
| is_allow_foreign | tinyint(1)       | NO   |     | 0       |                | 
+------------------+------------------+------+-----+---------+----------------+

The goal is to fetch a server (from mod_asterisk_servers) that has the enough channels available for a given date interval. This query

SELECT s.*,
       s.`channels_capacity` - IFNULL(SUM(b.`participants`), 0) as 'channels_available'
  FROM `mod_asterisk_servers` as s 
  LEFT JOIN `mod_asterisk_booking` as b ON (b.server_id=s.id AND (b.date_call BETWEEN '2011-07-30 15:15:00' AND '2011-07-30 17:15:00'))   
 GROUP BY s.id  
 ORDER BY 'channels_available' DESC;

could return something like :

+----+-------------+-----+------------------+--------------------+
| id | name        | ... |channels_capacity | channels_available |
+----+-------------+-----+------------------+--------------------+
|  1 | Test server | ... |              150 |                140 | 
+----+-------------+-----+------------------+--------------------+

Now, I’d like to add some columns to this query; notably the phone numbers associated with each server found. A phone number may have these combination :

  • local phone number (is_toll_free=0 AND is_allow_foreign=0)
  • toll free number, limited to a given region (is_toll_free=1 AND is_allow_foreign=0)
  • toll free number, allowing an “extended” region (is_toll_free=1 AND is_allow_foreign=1)

I tried this query

SELECT s.*,
       s.`channels_capacity` - IFNULL(SUM(b.`participants`), 0) as 'channels_available', 
       count(p1.phone_number) as 'local_phones',
       count(p2.phone_number) as 'toll_free_phones',
       count(p3.phone_number) as 'allow_foreign_phones' 
  FROM `mod_asterisk_servers` as s 
  LEFT JOIN `mod_asterisk_booking` as b ON (b.server_id=s.id AND (b.date_call BETWEEN '2011-07-30 15:15:00' AND '2011-07-30 17:15:00')) 
  LEFT JOIN `mod_asterisk_server_phones` as p1 ON (p1.server_id=s.id AND p1.is_toll_free=0 AND p1.is_allow_foreign=0) 
  LEFT JOIN `mod_asterisk_server_phones` as p2 ON (p2.server_id=s.id AND p2.is_toll_free=1 AND p2.is_allow_foreign=0) 
  LEFT JOIN `mod_asterisk_server_phones` as p3 ON (p3.server_id=s.id AND p3.is_toll_free=1 AND p3.is_allow_foreign=1) 
 ORDER BY 'channels_available' DESC;

but it returns

+----+-------------+-----+-------------------+--------------------+--------------+------------------+----------------------+
| id | name        | ... | channels_capacity | channels_available | local_phones | toll_free_phones | allow_foreign_phones |
+----+-------------+-----+-------------------+--------------------+--------------+------------------+----------------------+
|  1 | Test server | ... |               150 |                140 |            2 |                2 |                    2 | 
+----+-------------+-----+-------------------+--------------------+--------------+------------------+----------------------+

even though there are only three numbers for that server :

mysql> select * from mod_asterisk_server_phones where server_id = 1;
+----+-----------+----------------+-------------+-----------+--------------+------------------+
| id | server_id | phone_number   | phone_alias | extension | is_toll_free | is_allow_foreign |
+----+-----------+----------------+-------------+-----------+--------------+------------------+
|  1 |         1 | XXX-XXX-XXXX   |             |      XXXX |            0 |                0 | 
|  2 |         1 | 1-800-XXX-XXXX |             |      XXXX |            1 |                0 | 
|  3 |         1 | 1-800-XXX-XXXX |             |      XXXX |            1 |                1 | 
+----+-----------+----------------+-------------+-----------+--------------+------------------+

Maybe someone with better understanding of SQL can help me figure out this one?

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-05-23T21:08:16+00:00Added an answer on May 23, 2026 at 9:08 pm

    Try count(DISTINCT p1.phone_number) instead of count(p1.phone_number) (and the same for p2,p3). And don’t forget the proper GROUP BY

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

Sidebar

Related Questions

I have the follow two mysql tables: mysql> desc subscriptions; +------------+--------------+------+-----+---------+----------------+ | Field |
I have two mysql tables and i want to merge the results of these
I have these tables: customer -------- customer_id int name varchar(255) order ----- order_id int
I have these tables: Projects(projectID, CreatedByID) Employees(empID,depID) Departments(depID,OfficeID) Offices(officeID) CreatedByID is a foreign key
Suppose you have these tables: Table Name: Salesman Fields: S_ID(Primary Key), Name Table Name:
In MySQL I have these 3 tables : CREATE TABLE IF NOT EXISTS Seasons
I have in my MySQL database these two tables: CREATE TABLE IF NOT EXISTS
I have a MySQL table where there is a 'date_added' (date) field, 'time_added' (time)
I have these 3 tables + data: items : itemId, itemName data: 1, my
If I have these two tables: <table> <tr> <td>Small Length Content</td> </tr> </table> <table>

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.