开发者

PHP: Fetching data from *strange* MySQL database

开发者 https://www.devze.com 2023-04-02 10:56 出处:网络
I have a closed-source software written in C#/.NET from a VoIP company which is impossible to customize and wanted to create custom front-end using PHP. I gained access to the database and now see how

I have a closed-source software written in C#/.NET from a VoIP company which is impossible to customize and wanted to create custom front-end using PHP. I gained access to the database and now see how it functions. I wanted to output the user his 'speed dial' numbers, but having issue solving it. Here are the tables structures:

'customer' table
+-------------------------------------------------------------------------+
| CustomerID | FirstName | LastName | Balance | Email | Password | Status |
|-------------------------------------------------------------------------|
| 1          | Homer     | Simpson  | 5.00    | h@s.s | iheartm  | 1      |
| 2          | Marge     | Simpson  | 3.00    | m@s.s | ihearth  | 1      |
+-------------------------------------------------------------------------+

'calls' table
+------------------------------------------------------------------------+
| CallID | Caller  | Callee  | ServiceID  | Duration | Cost | CustomerID |
|------------------------------------------------------------------------|
| 1      | 1234567 | 7654321 | 30         | 60       | 1.00 | 1          |
| 2      | 7654321 | 1234567 | 45         | 120      | 2.00 | 2          |
+----------------------------------------------------开发者_开发问答--------------------+

'ani' (speed-dial) table
+---------------------------------------+
| PhoneNumber | ServiceID | ContactName |
|---------------------------------------|
| 1234567     | 45        | Homer       |
| 7654321     | 30        | Marge       |
+---------------------------------------+

As you can see, 1234567 is Homer's phone number and in Marge's speed dial list and 7654321 is Marge's number in Homer's list. Just like I can pull up customer's balance when logged in using: $current_user['Balance'];, is there way to show user his 'speed dial' numbers in PHP?


This request doesn't achieve what you want ?

SELECT 
    a.CustomerID, a.FirstName, a.LastName, a.Balance, a.Email, a.Status,
    b.ServiceID,
    (SELECT GROUP_CONCAT(CONCAT(ContactName,':',PhoneNumber)) FROM ani GROUP BY PhoneNumber WHERE ServiceID = b.ServiceID)
FROM customer a
LEFT JOIN calls b ON a.CustomerID = b.CustomerID
WHERE a.CustomerID = 'replace_by_customer_id'

This should fetch the data of the customer table, plus a string that results from concatenating the speed-dial numbers of the customer connected.

I assume that a customerid corresponds to one unique serviceid found in calls, and the serviceid in the table ani indicates the owner of the speed-dial number. But it seems a weird architecture, so you should give us more data or informations about the tables...


There seems to be a missing relation from ServiceID to a specific entry in customer. It would seem strange that the calls table would provide that relation, it should merely use it.

With only the information you supplied you can only join calls to link the CustomerID with the ServiceID which I suppose you have. The query would look like this:

SELECT ContactName, PhoneNumber FROM ani
LEFT JOIN calls ON ani.ServiceID=calls.ServiceID
WHERE calls.CustomerID=xxx
0

精彩评论

暂无评论...
验证码 换一张
取 消