如何从12个表中获取非匹配记录

时间:2016-06-30 08:05:18

标签: mysql sql

我正在使用下面的代码,但我没有得到任何结果,请任何人都可以给我一个建议。

users_register表包含所有用户的手机号码,我有12个表格,如propertyhostelshotels等等。

如何获得不匹配的手机号码

SELECT t1.user_mobileno,
       t1.user_fullname
FROM users_register t1
LEFT JOIN userpostproperties t2 ON t1.user_mobileno = t2.property_contact_number
LEFT JOIN services t3 ON t1.user_mobileno=t3.contact_num
LEFT JOIN hostels t4 ON t1.user_mobileno=t4.h_contact_num
LEFT JOIN sales t5 ON t1.user_mobileno=t5.sales_contact
LEFT JOIN rentals t6 ON t1.user_mobileno=t6.rental_mobile
LEFT JOIN jobs t7 ON t1.user_mobileno=t7.emp_mobile
LEFT JOIN healthcare t8 ON t1.user_mobileno=t8.contact_num
LEFT JOIN education t9 ON t1.user_mobileno=t9.edu_mobile
LEFT JOIN hotels t10 ON t1.user_mobileno=t10.contact_num
LEFT JOIN restaurant t11 ON t1.user_mobileno=t11.contact_num
LEFT JOIN finance t12 ON t1.user_mobileno=t12.contact_no
LEFT JOIN manufacturer t15 ON t1.user_mobileno=t15.contact_no

1 个答案:

答案 0 :(得分:1)

要从users_register表中获取不匹配的记录,您可以在LEFT JOIN子句中使用IS NULL列名为WHERE的{​​{1}}。

这是你期待的吗?

SELECT t1.user_mobileno,
       t1.user_fullname
FROM users_register t1
LEFT JOIN userpostproperties t2 ON t1.user_mobileno = t2.property_contact_number
LEFT JOIN `services` t3 ON t1.user_mobileno = t3.contact_num
LEFT JOIN hostels t4 ON t1.user_mobileno = t4.h_contact_num
LEFT JOIN sales t5 ON t1.user_mobileno = t5.sales_contact
LEFT JOIN rentals t6 ON t1.user_mobileno = t6.rental_mobile
LEFT JOIN jobs t7 ON t1.user_mobileno = t7.emp_mobile
LEFT JOIN healthcare t8 ON t1.user_mobileno = t8.contact_num
LEFT JOIN education t9 ON t1.user_mobileno = t9.edu_mobile
LEFT JOIN hotels t10 ON t1.user_mobileno = t10.contact_num
LEFT JOIN restaurant t11 ON t1.user_mobileno = t11.contact_num
LEFT JOIN finance t12 ON t1.user_mobileno = t12.contact_no
LEFT JOIN manufacturer t15 ON t1.user_mobileno = t15.contact_no
WHERE   t2.property_contact_number IS NULL AND 
        t3.contact_num IS NULL AND 
        t4.contact_num IS NULL AND 
        t5.sales_contact IS NULL AND 
        t6.rental_mobile IS NULL AND
        t7.emp_mobile IS NULL AND
        t8.contact_num IS NULL AND
        t9.edu_mobile IS NULL AND
        t10.contact_num IS NULL AND
        t11.contact_num IS NULL AND
        t12.contact_no IS NULL AND
        t15.contact_no IS NULL