Solved

MYSQL: How to combine two queries

Posted on 2013-01-10
3
617 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

627 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