PHP - MYSQL在sql中不存在

时间:2015-02-20 22:32:14

标签: php mysql

我有我当前的SQL查询:

$get_appts = mysql_query("SELECT 
        contact.contact_sms, contact.contact_email, appointments.pet_id, appointments.app_uniq, appointments.appointment_confirmed, appointments.client_id, pets.pet_name, cust.go_sms, cust.go_email, cust.first, cust.last, appointments.clinic_id, practice.clinic_phone, practice.clinic_phone, practice.clinic_img, practice.clinic_name, appointments.appointment_longid, appointments.appointment_date, appointments.appointment_time, appointments.appointment_confirmed FROM appointments 
        LEFT JOIN contact ON appointments.client_id=contact.client_id 
        LEFT JOIN pets ON appointments.client_id=pets.client_id
        LEFT JOIN cust ON appointments.client_id=cust.id
        LEFT JOIN practice ON appointments.clinic_id=practice.clinic_id
        WHERE (appointments.appointment_date='$check_date' AND (appointments.appointment_confirmed !='2' OR appointments.appointment_confirmed != '3') AND contact.participate='1') AND (practice.clinic_id ='$run_clinic_id')");

自从项目开始以来,这一直在为我的项目工作,但现在我需要做出改变,我不知道该如何去做。

我有一个名为contact_record的表,它存储了通过系统发送的先前发送的SMS消息的列表。我现在需要修改现有的sql查询以获取相同的信息,但是从WHERE上面的所有内容和记录在contact_record表中都不存在。

我可以用另一个sql查询和一些逻辑来处理这个问题,但是我一直在绞尽脑汁用一个查询完成这一切。那可能吗? 我找不到任何mysql代码:

Select * FROM table WHERE table.column = '$var_to_test_against' DOES NOT EXIST IN TABLE

如果有人能帮我解决问题,谢谢你!

1 个答案:

答案 0 :(得分:0)

这不是一个答案,但它是一个开始......你说你现有的查询是有效的,在这种情况下它可以重写如下:

SELECT c.contact_sms
     , c.contact_email
     , a.pet_id
     , a.app_uniq
     , a.appointment_confirmed
     , a.client_id
     , p.pet_name
     , cust.go_sms
     , cust.go_email
     , cust.first
     , cust.last
     , a.clinic_id
     , k.clinic_phone
     , k.clinic_phone
     , k.clinic_img
     , k.clinic_name
     , a.appointment_longid
     , a.appointment_date
     , a.appointment_time
     , a.appointment_confirmed
  FROM appointments a 
  JOIN contact c
    ON c.client_id = a.client_id 
  LEFT 
  JOIN pets p
    ON p.client_id = a.client_id 
  LEFT 
  JOIN cust 
    ON cust.id = a.client_id 
  JOIN practice k
    ON k.clinic_id = a.clinic_id 
 WHERE a.appointment_date =  $check_date
   AND a.appointment_confirmed NOT IN (2,3) 
   AND c.participate = 1
   AND k.clinic_id = $run_clinic_id