Solved

MYSQL: How to combine two queries

Posted on 2013-01-10
3
609 Views
Last Modified: 2013-01-11
Dear Experts.

I have two tables.

Table 1 has the following columns.

id
host
count

The following query sets the count field to 3 where the host is microsoft.com

UPDATE hosts SET `count` =  '3' WHERE  hosts.host = "microsoft.com";  

Open in new window


Table 2 has the following columns.

id
host
company_emails
sent

This query provides a list of hosts with the corresponding count of emails sent

SELECT `host` , COUNT( * ) AS total FROM  `company_emails` WHERE `sent` = 1 GROUP BY host ORDER BY COUNT( * ) DESC;

Open in new window


How would I combine the two queries so that hosts.count field contains the "total" value calculated in the second query?

Many thanks.
0
Comment
Question by:AdrianSmithUK
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
jessegivy earned 500 total points
ID: 38765279
Maybe this?

UPDATE
hosts,
(SELECT host, COUNT( * ) as total FROM  `company_emails` WHERE `sent` = 1 GROUP BY host) as emails
SET hosts.`count` =  emails.total WHERE  hosts.host = email.host;
0
 

Author Closing Comment

by:AdrianSmithUK
ID: 38766326
Almost perfect. A small typo (email.host) should read emails.host.

You're a star!

Many thanks.
Adrian
0
 

Author Comment

by:AdrianSmithUK
ID: 38766329
Final solution: (in case I need to cut and paste it at some time)

UPDATE HOSTS ,
(
SELECT host, COUNT( * ) AS total
FROM  `company_emails` 
WHERE  `sent` =1
GROUP BY host
) AS emails
SET hosts.`count` = emails.total WHERE hosts.host = emails.host

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question