Solved

MYSQL: How to combine two queries

Posted on 2013-01-10
3
602 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Latency in .net app using DB in .net 21 36
Amazon Redshift 2 28
updating the date data 12 25
Need assistance with TXT file export.  While Loop has issue. 1 19
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now