简体   繁体   中英

How to get data from 3 different mysql tables with one query

My question is that I need to get a data (email) from different tables with one query. I have searched and I couldnt find enough information so I decided to ask here.

The idea is to send email to certain people from different departments and also send the email to everybody in all departments. My code is below;

if ($_POST['recipient'] == 'parents'){$query = "SELECT `email`, `first_name` FROM `users` WHERE `allow_email` = 1 AND `active` = 1";}
        if ($_POST['recipient'] == 'teachers'){$query = "SELECT `email`, `name` FROM `teachers` WHERE `status` = 1";}
        if ($_POST['recipient'] == 'staff'){$query = "SELECT `email`, `name` FROM `staff`";}


        $result = $con->query($query);
        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {

            $mail = new PHPMailer();
            $mail->IsHTML(true);
            $mail->SMTPAuth   = true;                  // enable SMTP authentication
            $mail->Host       = $site_settings['smtp_host']; // sets the SMTP server
            $mail->Port       = 26;                    // set the SMTP port
            $mail->Username   = $site_settings['smtp_username']; // SMTP account username
            $mail->Password   = $site_settings['smtp_password']; // SMTP account password
            $email->From      = $site_settings['school_email'];
            $email->FromName  = $site_settings['school_name'];
            $email->Subject   = 'Newsletter: '.$_POST['subject'];
            $email->Body      = $_POST['body'];
            $email->AddAddress( $row['email'] );

            $email->Send(); 
        }

Could anyone help?

I suppose you could create a view in MySQL database and aggregate data for one entity like this:

CREATE VIEW `all_user_emails` (email, name, type) AS 
SELECT `email`, `first_name`, 'user' FROM `users` WHERE `allow_email` = 1 AND `active` = 1
UNION
SELECT `email`, `name`, 'teacher' FROM `teachers` WHERE `status` = 1
UNION
SELECT `email`, `name`, 'staff' FROM 'staff'

Then you may query this view like a table like this

SELECT * FROM all_user_emails WHERE type = `staff`

You can use a UNION between the three tables to get one result set. The query would look like this:

SELECT `email`, `first_name`, 'user' FROM `users` WHERE `allow_email` = 1 AND `active` = 1
UNION
SELECT `email`, `name`, 'teacher' FROM `teachers` WHERE `status` = 1
UNION
SELECT `email`, `name`, 'staff' FROM `staff`

Your PHP code might look like this:

if ($_POST['recipient'] == 'parents'){$query = "SELECT `email`, `first_name` FROM `users` WHERE `allow_email` = 1 AND `active` = 1";}
elseif ($_POST['recipient'] == 'teachers'){$query = "SELECT `email`, `name` FROM `teachers` WHERE `status` = 1";}
elseif ($_POST['recipient'] == 'staff'){$query = "SELECT `email`, `name` FROM `staff`";}
elseif ($_POST['recipient'] == 'all')($query="SELECT `email`, `first_name`, 'user' FROM `users` WHERE `allow_email` = 1 AND `active` = 1 UNION SELECT `email`, `name`, 'teacher' FROM `teachers` WHERE `status` = 1 UNION SELECT `email`, `name`, 'staff' FROM `staff`")

The column names from the query would be the column names from the first SELECT of the UNION, so to access the value that would contain 'teacher', 'staff' or 'user' you would use $row['user'] and if you wanted to use the person's name in your code you would use $row['first_name'] , not $row['name'] , but you could change that for consistency and clarity by starting out with:

SELECT `email`, `first_name` as name, 'user' as type FROM `users` ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM