How do I get information from multiple MySQL tables? - mysql

I'm working on a way to keep track of physical connections between servers/switches in multiple racks. I set up my database so it is like this:
mysql> show tables;
+----------------------+
| Tables_in_System |
+----------------------+
| connections |
| racks |
| servers |
+----------------------+
mysql> select * from racks;
+----+-------------+-------------+----------+
| id | rack_number | rack_height | location |
+----+-------------+-------------+----------+
| 2 | 7430 | 43 | xxx |
| 3 | 7431 | 43 | xxx |
| 4 | 7432 | 43 | xxx |
+----+-------------+-------------+----------+
mysql> select * from servers;
+----+-------------+---------------+-----------------+---------------+
| id | server_type | rack_location | server_unit_loc | server_height |
+----+-------------+---------------+-----------------+---------------+
| 1 | Server 1 | 7430 | 2 | 3 |
| 2 | Cisco 2960 | 7431 | 1 | 1 |
| 3 | Server 2 | 7431 | 9 | 1 |
| 15 | Server 3 | 7432 | 27 | 2 |
| 16 | Cisco 2248 | 7432 | 29 | 1 |
+----+-------------+---------------+-----------------+---------------+
rack_location refers to the numbered racks in the first table.
server_unit_loc is the placement of the server in the rack, 1 being the top, 43 would be the bottom. And server_height is just how tall the server is.
mysql> select * from connections;
+----+----------+----------+------------+------------+---------+---------+------------+
| id | rack_id1 | rack_id2 | server_id1 | server_id2 | port_s1 | port_s2 | cable_type |
+----+----------+----------+------------+------------+---------+---------+------------+
| 1 | 7430 | 7430 | 2 | 1 | J01 | J08 | RJ-45 |
| 2 | 7430 | 7431 | 2 | 3 | J03 | J08 | RJ-45 |
| 3 | 7430 | 7432 | 2 | 15 | J02 | J09 | SFP+ |
+----+----------+----------+------------+------------+---------+---------+------------+
rack_id's again (I realize now these are probably pointless...) server_id1 & 2 are the id's from the servers table. ports_1&2 correlate to the jack the cable is plugged in. This is an internal system we use, so J01 means it is the first jack on the server, and it plugs into the 8th(J08) port on the connecting server.
The end goal here is to be able to quickly see how many servers are connected to any chosen server and also the details about that connection info. In this example I connected the Cisco 2960 to multiple servers in different racks.
+What kind of query would I have to put in to return all the servers that are connected to the Cisco (2). I'd like to be able to specify just the server_id# and it would go to the different tables and get all the information such as which rack it's in, it's type, and the height of the server
Cisco2960 7430 A01 J01 --> Server1 7430 A02 J08 "RJ-45"
Cisco2960 7430 A01 J03 --> Server2 7431 A09 J08 "RJ-45"
Cisco2960 7430 A01 J02 --> Server3 7432 A27 J09 "SFP+"
Something like that...^
Thanks! Also if you have a better idea of how to go about accomplishing this I'd love to hear everyone's thoughts.

You would need to create query that joins the related information together.
Think of label S being (source server). Label T being (target server).
Ex.
SELECT S.server_type, S.rack_location, CONCAT('A0',S.server_unit_loc), B.port_s1,T.server_type, T.rack_location, CONCAT('A0',T.server_unit_loc), B.port_s2,B.cable_type FROM servers S
JOIN connections B ON B.server_id1 = S.id
JOIN servers T ON B.server_id2 = T.ID
WHERE S.id = 2
I agree with one of the comments. You could normalize things (rid some redundant rack information).

Related

SQL join: selecting last records from duplicates

I am working on python project "Linux Server Monitor". When you load monitoring website you create your servers and table look like this:
Servers:
+-----------+-----------+---------+-------------+
| server_id | auth_code | user_id | server_name |
+-----------+-----------+---------+-------------+
| 0 | xyz123 | 1 | My Server 1 |
| 1 | aabb | 45 | BlaBlaUser |
| 2 | asdfh1 | 1 | My Server 5 |
+-----------+-----------+---------+-------------+
After the server is created, you start python script and it starts to upload monitoring data to this table (where all data is stored, from all users):
Monitoring Data:
+----+-----------+---------+---------+------------+-----------------+------------------+
| id | auth_code | user_id | os_name | os_version | external_ip | some_other_table |
+----+-----------+---------+---------+------------+-----------------+------------------+
| 0 | aabb | 45 | Ubuntu | 16.04 | 77.88.99.100 | dynamic data |
| 1 | aabb | 45 | Ubuntu | 16.04 | 77.88.99.100 | dynamic data 2 |
| 2 | aabb | 45 | Ubuntu | 16.04 | 10.10.10.10 | dynamic data 3 |
| 3 | xyz123 | 1 | Fedora | 27 | 92.94.95.99 | different data 1 |
| 4 | xyz123 | 1 | Fedora | 27 | 92.94.95.99 | different data 2 |
| 5 | xyz123 | 1 | Fedora | 27 | 92.94.95.99 | different data 3 |***
| 6 | asdfh1 | 1 | CentOS | 7 | 5.5.5.5 | some new data 1 |
| 7 | asdfh1 | 1 | CentOS | 7 | 5.5.5.5 | some new data 2 |
| 8 | asdfh1 | 1 | CentOS | 7 | 2.2.2.2 | some new data 3 |###
+----+-----------+---------+---------+------------+-----------------+------------------+
I want to join first and second table to show user "important" information like this:
Your Active Servers:
+-----------+-------------+---------+------------+-----------------+
| server_id | server_name | os_name | os_version | external_ip |
+-----------+-------------+---------+------------+-----------------+
| 0 | My Server 1 | Fedora | 27 | 92.94.95.99 |***
| 2 | My Server 5 | CentOS | 7 | 2.2.2.2 |###
+-----------+-------------+---------+------------+-----------------+
If I join first and second table by user_id it will load duplicates too but I want only last inserted record of that particular user's server. In this case on last record "external_ip" of one server changed but other one remained the same (marked with *** and ###).
you could use an inner join on subselect for the max id for user, server on monitorig
select s.server_id, s.server_name, m.os_name, m.os_version, m.external_ip
from servers s
inner join Monitoring m on m.user_id = s.user_id and s.auth_code = m.auth_code
inner join (
select max(id) max_id, user_id, os_name, os_version
from Monitoring
group by user_id, os_name, os_version
) t on t.max_id = m.id
where user_id = 1

Query date from same column with several conditions

I want to do a query to see when a client is disconnected, between January and February, by a cable company and if they got connect again until 7 days after the disconnection.
My table would be something like this:
+---------+----------+------------+------------+
| client | ID_Order | Work_Order | Date_Order |
+---------+----------+------------+------------+
| Client1 | 123AB | Disconnect | 20/01/2017 |
| Client1 | 234EA | Connect | 22/01/2017 |
| Client2 | 242FA | Connect | 30/01/2017 |
| Client3 | 244FE | Disconnect | 30/01/2017 |
| Client2 | 301EA | Disconnect | 10/02/2017 |
| Client4 | 355AD | Disconnect | 20/02/2017 |
| Client4 | 368AD | Connect | 25/02/2017 |
| Client5 | 401AD | Connect | 05/03/2017 |
| Client6 | 440AD | Disconnect | 15/03/2017 |
+---------+----------+------------+------------+
And I want to transform the previous table in a table like this: one row per client, with the connect order and disconnect order in 2 different columns
+---------+-----------------+-------------+--------------+------------+
| client | ID_Order_Discon | Date_Discon | ID_Order_Con | Date_Con |
+---------+-----------------+-------------+--------------+------------+
| Client1 | 123AB | 20/01/2017 | 234EA | 22/01/2017 |
| Client4 | 355AD | 20/02/2017 | 368AD | 25/02/2017 |
+---------+-----------------+-------------+--------------+------------+
My problem:
1- How make the query inside the same columns (Date_Order), to choose the clients that where connected after 7 days the disconnection. Guess need to use DATEDIFF()?
2- how to put the orders of the same client in 2 different columns? Guess I have to do an IF()/CASE () and a join ()?
To Combine both table you can use:
select c1.client, cq.ID_Order ID_Order_Discon , c1.Date_Order as Date_discon, c2.ID_Order ID_Order_Con, c2.Date_order Date_Con
from table c1 join table c2 on t1.client = t2.client and t1.work-order = 'disconnect' and t2.work_order = 'Connect'
and for the frst question you can use where DATEDIFF(c1.date_order,c2.dateOrder)>7

Update table from another table throught intermediate table Mysql

I have the next table setup, two tables related by an intermediate table, like this:
Client
| client_id | ...|field_X |
| 1 | ...|value1 |
| 2 | ...|value2 |
| 3 | ...|value3 |
Project
| project_id | ...|field_X |
| 1 | ...| |
| 2 | ...| |
| 3 | ...| |
| 4 | ...| |
| 5 | ...| |
| 6 | ...| |
| 7 | ...| |
client_project
| client_id | project_id|
| 1 | 2 |
| 1 | 3 |
| 2 | 4 |
| 2 | 5 |
| 3 | 6 |
| 3 | 7 |
The field_x in the table project is new and i have to fill it with the data from table client to get approximately something like this:
Project
| project_id | ...|field_X |
| 1 | ...| |
| 2 | ...|value1 |
| 3 | ...|value1 |
| 4 | ...|value2 |
| 5 | ...|value2 |
| 6 | ...|value3 |
| 7 | ...|value3 |
i dont know hot to deal with the intermediate table. i have tried this code but it doesnt work.
INSERT INTO project
(field_x)
(select field_x
from
client_project
inner join
client
where client_project.client_id = client.client_id
);
I have the idea of what i have to do but i am not able to translate it into a sql command because of the intermeditate table.Could someone explain how to deal with it?
Thanks in advance.
I assume you already have all entries in the project table but they're missing the Field_X property? So what you need is an update, not an insert
UPDATE project p, client c, project_client pc SET p.Field_X=c.Field_X WHERE p.ID=pc.ProjectID AND c.ID=pc.ClientID
However, be advised that having the same data in two places is not a good practise; if possible always put one fact in only one place.

how to insert average of a query into mysql table

I am trying to get average of latency for each items that holds into two separate mysql table. Let me more clarify that I have two mysql tables as below,
table: monitor_servers
+-----------+-----------------+
| server_id | label |
+-----------+-----------------+
| 1 | a.com |
| 2 | b.com |
+-----------+-----------------+
table: monitor_servers_uptime
+-------------------+-----------+-----------+
| servers_uptime_id | server_id | latency |
+-------------------+-----------+-----------+
| 1 | 1 | 0.4132809 |
| 3 | 1 | 0.4157769 |
| 6 | 1 | 0.4194210 |
| 9 | 1 | 0.4140880 |
| 12 | 2 | 0.4779439 |
| 15 | 2 | 0.4751789 |
| 18 | 2 | 0.4762829 |
| 22 | 2 | 0.4706681 |
+-------------------+-----------+-----------+
Basically, each domains associated with the same id_number in both tables. While I am running the query below, getting average of each items.
select monitor_servers.label, avg(monitor_servers_uptime.latency)
from monitor_servers,monitor_servers_uptime
where monitor_servers.server_id = monitor_servers_uptime.server_id
group by monitor_servers.server_id;
The query ended up,
+---------------------+-------------------------------------+
| label | avg(monitor_servers_uptime.latency) |
+---------------------+-------------------------------------+
| a.com | 0.41393792995 |
| b.com | 0.47551423171 |
+---------------------+-------------------------------------+
My questions are doing am i in wright way while getting average of the each items and how can i insert new average result of each items into a new column on table monitor_servers ? And also what happens if some of latency rows are NULL ?
**Edit : What i am trying to achieve in one query result is **
+-----------+----------+------------------+
| server_id | label | avg. |
+-----------+----------+------------------+
| 1 | a.com | 0.41393792995 |
| 2 | b.com | 0.47551423171 |
+-----------+-----------------------------+
Thanks in advance,
Your calculation seems to be correct.
You could add another column to the monitor_servers using sql:
ALTER TABLE monitor_servers ADD avg_latency DEFAULT 0.0 NOT NULL
For doing the AVG calculation check this answer.

MySQL Newbie - Select all False in One to Many

Devices Network_Card
================== ===========================================
| id | hostname | | id | device_id | ip_address | dns |
================== ===========================================
| 1 | desktop1 | | 1 | 1 | 10.0.0.1 | desktop1 |
| 2 | laptop1 | | 2 | 2 | 10.0.0.2 | laptop1 |
| 3 | laptop2 | | 3 | 2 | 10.0.0.3 | laptop1w |
| 4 | desktop2 | | 4 | 3 | 10.0.0.4 | george |
| .. | ... | | 5 | 4 | 10.0.0.5 | desktop2w |
================== ===========================================
Hi folks, here's my situation. We have a home grown computer inventory with 500+ devices and the db gets populated from different sources. I'm trying to find the discrepancies in this case, so I'm trying to select devices.id where I don't have a DNS record is NOT LIKE the hostname. In this case, it would return devices.id = 3
I haven't done relational db queries in a long time, and my brain can't process the double negatives, so any help would be great.
This query might do it for you:
select d.id, d.hostname, n.dns
from devices d
left join network_card n on d.id = n.device_id
where d.hostname != substring(n.dns, 1, LENGTH(d.hostname))
I'm using substring since I'm not sure how to do a field + wildcard to use within a NOT LIKE expression. Using substring lets you do a straight != comparison.
Update: Thanks to PaparazzoKid for the sqlfiddle at: http://www.sqlfiddle.com/#!2/3a260/1