  | 
              
	              
	                
  
    
	 
        ปัญหา Join Table 2 ตาราง หาค่าที่ไม่มีในอีก table 1 ทำไงดีครับ รบกวนหน่อย     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 เอา a เป็นหลัก ที่ไม่มีใน b 
Code (SQL) 
select a.id, b.id from a left join b on a.id=b.id where b.id is null 
  
 
เอา b เป็นหลัก ที่ไม่มีใน a 
Code (SQL) 
select a.id, b.id from a right join b on a.id=b.id where a.id is null 
  
 
เอา a ที่ไม่มี ใน b และ b ที่ไม่มีใน a 
Code (SQL) 
select a.id, b.id from a left join b on a.id=b.id where b.id is null 
union
select a.id, b.id from a right join b on a.id=b.id where a.id is null 
  
 
หรือ จะใช้  right outer , left outer , full outer ก็ได้ ลองเปิดหาวิธีการใช้ดูนะครับ                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2015-01-04 20:38:43 2015-01-04 20:39:03 2015-01-04 20:40:40 2015-01-04 20:45:20              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2015-01-04 20:38:17 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ไม่ได้ ทั้ง 3 ชุดเลยครับ ยังงง อยู่ว่า ทำไมมันไม่ออก  
 
ชุดนี้ สามารถดึงข้อมูล ใน Table A ออกมาได้ แต่ไม่ตรงตามจุดประสงค์ อยากให้เฉพาะ material_pid นั้นๆ 
Code (PHP) 
SELECT * FROM categories left join product_material on categories.categories_id=product_material.material_cid 
WHERE product_material.material_cid is null
  
  
 
 
แต่พอผมเพิ่มเงื่อนไข material_pid='196' ลงไป คราวนี้ไม่มีข้อมูลออกมาเลยครับ 
Code (PHP) 
SELECT * FROM categories left join product_material on categories.categories_id=product_material.material_cid 
WHERE product_material.material_cid is null and product_material.material_pid='196'
  
  
 
 
 
แก้ไขยังไงดี ครับ รบกวนพี่ๆหน่อยครับ                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2015-01-04 22:19:52              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2015-01-04 22:18:15 | 
                        By :
                            ilikeit | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (SQL) 
SELECT DISTINCT
	categories.categories_id,
	categories.categories_name
FROM 
	categories INNER JOIN product_material ON categories.categories_id=product_material.material_cid 
WHERE
	product_material.material_pid=196
                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2015-01-04 22:40:38 | 
                        By :
                            Krungsri | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ต้องการ b 196 ที่ไม่มี ใน a หรือเปล่าครับ 
อันนี้จะได้ record สูงสุดคือ b 196 ทั้งหมด ที่ ไม่พบใน a 
Code (SQL) 
select b.* from product_material b 
left join categories a on a.category_id=b.material_cid
where b.material_pid=196 and a.category_id is null
  
 
 
 
 
หรือ ว่า a ที่ ไม่มี ใน b 196 
อันนี้จะได้ record สูงสุด คือ a ทั้งหมด ที่ไม่ตรง กับ b 196 
 
Code (SQL) 
select a.* from categories 
left join product_material b on a.category_id=b.material_cid and b.material_pid=196
where b.material_pid is null
                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2015-01-04 22:51:16 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ขอบคุณครับ พี่ Krungsri 
  
ผมจะพยายามไปเรื่อยๆ ผลลัพธ์คือ 
  
 
 
ซึ่งมันจะต้องแสดง categories  ที่ไม่มีใน material_pid='196' ออกมา อ่ะคับ แต่ขอบคุณนะคับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2015-01-04 22:53:09 | 
                        By :
                            ilikeit | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 "ไม่มีใน" ก็ลองดูคำสั่งนี้ 
Go to : SQL NOT IN                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2015-01-04 22:59:18 | 
                        By :
                            Krungsri | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 SELECT a.id, b.id  
FROM a  
INNER JOIN b ON a.id != b.id 
 
a มี 1,2,3,4,5 
b มี 2,3 
 
ค่าที่ได้จะเป็น 1,4,5 (ข้อมูลที่ไม่มีใน B) ใช้ในกรณีที่ตรวจสอบ เช่น สินค้าที่มีคนจองแล้ว โดยที่เราไม่ได้เพิ่ม ฟิลสถานะใน a แต่เช็คจาก ตารางการจอง(b) ได้เลย                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-02-09 14:38:17 | 
                        By :
                            Peerapat Surasing | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 SELECT a.*  
FROM  table_a a, table_b b 
WHERE a.a_id NOT IN (b.a_id)                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2018-04-04 12:14:34              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-04-04 12:13:50 | 
                        By :
                            chatpkt | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |