0

I'm selecting data from a table called "contacts" to grab email addresses. It's working just fine but I don't want to echo out any results if the email exists in another table called "scheduled_emails". Both tables have a column called "email", so I was thinking I could some how check some kinda comparison. I've been looking into the JOIN to combine rows. Am I wrong?

My script

$sql = "SELECT email FROM contacts WHERE username = '".$_SESSION["username"]."' ";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output emails of users account
while($row = $result->fetch_assoc()) {
    echo $row["email"];
  }
} else {
    echo "0 results";
}

Any help would be greatly appreciated :)

code
  • 1,041
  • 1
  • 12
  • 28
  • 1
    `"SELECT ct.email FROM contacts ct LEFT JOIN scheduled_emails se ON se.email= ct.email WHERE se.email IS NULL AND ct.username= ".$_SESSION["username"]` – Alive to die - Anant Mar 03 '20 at 08:21
  • 1
    SELECT email FROM contacts WHERE username = '".$_SESSION["username"]."' and email not in (select sched.email from scheduled_emails sched) – BUcorp Mar 03 '20 at 08:21
  • 1
    Try this `SELECT email FROM contacts WHERE username = '".$_SESSION["username"]."' and email not in (select email from scheduled_emails) ` – MiraTech Mar 03 '20 at 08:22
  • Thank you very much! Everyone :) I've tried this before - but obviously I did it wrong! I know I asked to not show them... but thinking maybe there is a quick way to show the emails but have them grayed out if they do exist in scheduled_emails? – code Mar 03 '20 at 08:28

0 Answers0