Solved

MYSQL: How to combine two queries

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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