2015-10-09 61 views
2

我的問題是我需要從一個查詢中獲取來自不同表的數據(電子郵件)。我搜查了,我找不到足夠的信息,所以我決定在這裏問。如何從一個查詢中獲取來自3個不同mysql表的數據

這個想法是發送電子郵件給不同部門的某些人,也發送電子郵件給所有部門的每個人。 我的代碼如下;

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(); 
     } 

任何人都可以幫忙嗎?

+0

表模式是怎樣的? – Akshay

+0

Parents表= user_id,用戶名,密碼,名字,姓氏,電子郵件,電話,email_code,活動,password_recover,類型,allow_email,個人資料。 教師=身份證,姓名,電話,電子郵件,身份 職員=身份證,姓名,電子郵件,電話,職位。 我想從所有表中獲取電子郵件數據... – Vurkac

+0

使用[UNION](https://dev.mysql.com/doc/refman/5.6/en/union.html)三個 –

回答

0

我想你可以像這樣一個實體創建MySQL數據庫和彙總數據的視圖:

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' 

然後你可以查詢這個視圖喜歡這樣

SELECT * FROM all_user_emails WHERE type = `staff` 
+0

我試過你的代碼,但沒有產生任何數據。 – Vurkac

+0

@Vurkac有沒有錯誤?這裏有一個錯字:'WHERE status ='(應該是'WHERE status = 1'),但除此之外,代碼看起來是正確的,似乎是你想要的。 –

+0

我修復了這個錯字,我得到了這個 - >調用一個非對象的成員函數fetch_assoc()。 代碼如下: $ SQL =「 CREATE VIEW'all_user_emails'(電子郵件,名稱,類型)AS 選擇'email','first_name', '用戶' 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' SELECT * FROM all_user_emails WHERE type ='staff' 「; $ result = $ con> query($ sql); \t //輸出每行的數據 while($ row = $ result-> fetch_assoc()){ echo $ row ['email']; } – Vurkac

0

您可以將表在三個表之間使用UNION來獲得一個結果集。查詢應該是這樣的:

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` 

你的PHP代碼可能是這樣的:

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`") 

從查詢中的列名是從UNION的第一個SELECT列名,因此訪問將包含'老師','工作人員'或'用戶'的值,您將使用$row['user'],如果您想在代碼中使用該人員的姓名,則可以使用$row['first_name']而不是$row['name'],但您可以更改以確保一致性和清晰度開始與:

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

謝謝你的回覆。但我得到這個錯誤:操作'UNION'的排序規則非法混用。但是當我刪除用戶的SELECT查詢時,一切正常。我認爲原因是教師和STAFF表具有相同數量的列,並且USERS表具有更多列。 有什麼辦法解決這個問題? – Vurkac

+0

這使事情變得複雜。我想這個答案也許比我能更好地描述問題:http://stackoverflow.com/questions/3029321/troubleshooting-illegal-mix-of-collat​​ions-error-in-mysql?rq=1 –

相關問題