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
Related
How can I get all the latest successful tests by program? The latest one has the highest Build number and successful are all PASSED and OF CONCERN
My table looks like this (I excluded some columns from the original):
+----+---------+----------------+-------+-----------+---------+
| ID | Test | Program | Build | Result | Tester |
+----+---------+----------------+-------+-----------+---------+
| 1 | 1 | Mag. & Speech | 1825 | PASSED | Dale |
| 2 | 2 | Scr. Reader | 1820 | PASSED | Aadarsh |
| 3 | 2 | Scr. Reader | 1821 | PASSED | Tony |
| 4 | 2 | Scr. Reader | 1824 | PASSED | Tony |
| 5 | 2 | Mag. & Speech | 1820 | PASSED | Colin |
| 6 | 2 | Mag. & Speech | 1821 | FAILED | Dale |
| 7 | 2 | Mag. & Speech | 1822 | OF CONCERN| Tony |
| 8 | 2 | Mag. | 1820 | PASSED | Steven |
| 9 | 3 | Scr. Reader | 1820 | NOT TESTED| Aadarsh |
+----+---------+----------------+-------+-----------+---------+
As a result I would want to get the row (ID) 1,4,7,8. As you can see, no program has more than one of the same test.
Edit:
Added some missing information to the table.
Sadly I don't have the queries anymore, I tried, but I didn't get very far with just Where and Order By.
This query should do the trick
SELECT t3.*
FROM (
SELECT t1.ID,
MAX(t1.Build) as Build
FROM table_name t1
WHERE LOWER(t1.Result) NOT IN( 'n/a', 'not completed', 'not tested' )
GROUP BY t1.Test, t1.Program
) t2
INNER JOIN table_name t3
ON t3.ID = t2.ID
AND t3.Build = t2.Build;
Unfortunately it is a bit complicated due to group by limitations.
Please replace table_name (in 2 places) with proper name
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).
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.
I am trying to run an MySQL query to copy over data from an old table (ps__product_review/rate) to a new table (ps_product_comment/grade) based on review ID (id_product_comment). But I am a bit lost on the SQL query, this is what I have but keep getting errors.
INSERT INTO ps_product_comment [(grade)]
SELECT rate
FROM ps__product_review
[WHERE ps__product_review.id_product_comment=ps_product_comment.id_product_comment];
Can anyone help write the correct query?
Edit:Essentially I am trying to populate the Grade column in the new table below.
Old table (ps__product_review)
+--------------------+----------+-----+
| id_product_comment | Comment | Rate|
+--------------------+----------+-----+
| 1 | Good | 2 |
| 2 | Great | 5 |
| 3 | OK | 3 |
| 4 | Brill | 4 |
| 5 | OK | 3 |
| 6 | Average | 2 |
| 7 | Bad | 1 |
+--------------------+----------+-----+
New Table (ps_product_comment)
+--------------------+----------+-------+
| id_product_comment | Comment | Grade |
+--------------------+----------+-------+
| 1 | Good | |
| 2 | Great | |
| 3 | OK | |
| 4 | Brill | |
| 5 | OK | |
| 6 | Average | |
| 7 | Bad | |
+--------------------+----------+-------+
If you want to update table with data from another table, use UPDATE with JOIN
UPDATE ps_product_comment
JOIN ps__product_review
ON ps__product_review.id_product_comment = ps_product_comment.id_product_comment
SET ps_product_comment.grade = ps__product_review.rate;
Remove the square brackets and I think you are missing the JOIN(since you are using that in your where clause):
INSERT INTO ps_product_comment (grade)
SELECT rate
FROM ps__product_review inner join ps_product_comment on
ps__product_review.id_product_comment=ps_product_comment.id_product_comment;
I'm new to relational sql. I'm trying to figure out a query to return the names of customers who have more than one type of account.
customers:
+------------+--------------+
| cid | Name |
+------------+--------------+
| 1 | Bob |
| 2 | John |
| 3 | Jane |
+------------+--------------+
accounts:
+------------+--------------+
| aid | type |
+------------+--------------+
| 1 | Checking |
| 2 | Saving |
| 3 | CD |
+------------+--------------+
transactions:
+------------+--------------+--------------+
| tid | cid | aid |
+------------+--------------+--------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 2 |
| 4 | 2 | 3 |
| 5 | 3 | 1 |
+------------+--------------+--------------+
With these tables, the query should return Bob and John. I'm having some trouble with how to write such a query. More specifically, how do I keep count of how many accounts a customer has and how do I compare if the accounts are different without adding a new column to the table?
Okay, this seems to work in SQL Fiddle with my test data structure. Try it out with your real data structure and see if it gives you what you're looking for.
SELECT name FROM customers c WHERE EXISTS(
SELECT DISTINCT aid FROM transactions
WHERE cid = c.cid
HAVING COUNT(DISTINCT aid)>1
)