MySQL subquery exercises

Test Data Table:

suppliers 表

  s_id  s_name          s_city     s_zip   s_call  
------  --------------  ---------  ------  --------
   101  FastFruit Inc.  Tianjin    300000  48075   
   102  LT Supplies     Chongqing  400000  44333   
   103  ACME            Shanghai   200000  90046   
   104  FNK Inc.        Zhongshan  528437  11111   
   105  Good Set        Taiyuang   030000  22222   
   106  Just Eat Ours   Beijing    010     45678   
   107  DK Inc.         Zhengzhou  450000  33332   

fruits table

f_id      s_id  f_name      f_price  
------  ------  ----------  ---------
a1         101  apple       5.20     
a2         103  apricot     2.20     
b1         101  blackberry  10.20    
b2         104  berry       7.60     
b5         107  xxxx        3.60     
bs1        102  orange      11.20    
bs2        105  melon       8.20     
c0         101  cherry      3.20     
l2         104  lemon       6.40     
m1         106  mango       15.60    
m2         105  xbabay      2.60     
m3         105  xxtt        11.60    
o2         103  coconut     9.20     
t1         102  banana      10.30    
t2         102  grape       5.30     
t4         107  xbababa     3.60   

The first statement, do not understand, in the end is to return the query results information table, or ture
( presumably point: . The SELECT * the FROM suppliers the WHERE Fruits s_id. = Suppliers s_idthe AND fruits.s_id = 107` statement, fruits and suppliers formed a en )

SELECT * FROM fruits
WHERE EXISTS
(SELECT * FROM suppliers 
WHERE fruits.`s_id` = suppliers.`s_id` AND  fruits.s_id=107);
f_id      s_id  f_name   f_price  
------  ------  -------  ---------
b5         107  xxxx     3.60     
t4         107  xbababa  3.60     

The following results of a query in the two tables:

SELECT * FROM suppliers ,fruits
WHERE fruits.`s_id` = suppliers.`s_id` AND  fruits.s_id=107
  s_id  s_name   s_city     s_zip   s_call  f_id      s_id  f_name   f_price  
------  -------  ---------  ------  ------  ------  ------  -------  ---------
   107  DK Inc.  Zhengzhou  450000  33332   b5         107  xxxx     3.60     
   107  DK Inc.  Zhengzhou  450000  33332   t4         107  xbababa  3.60     

The second statement, then the query returns ture, and then query the entire data table fruits
(guess points: the SELECT * Suppliers the WHERE s_id = the FROM statement Suppliers table 107 and the upper layer is not connected condition SELECT fruits )

SELECT * FROM fruits
WHERE EXISTS
(SELECT * FROM suppliers WHERE s_id=107);
f_id      s_id  f_name      f_price  
------  ------  ----------  ---------
a1         101  apple       5.20     
a2         103  apricot     2.20     
b1         101  blackberry  10.20    
b2         104  berry       7.60     
b5         107  xxxx        3.60     
bs1        102  orange      11.20    
bs2        105  melon       8.20     
c0         101  cherry      3.20     
l2         104  lemon       6.40     
m1         106  mango       15.60    
m2         105  xbabay      2.60     
m3         105  xxtt        11.60    
o2         103  coconut     9.20     
t1         102  banana      10.30    
t2         102  grape       5.30     
t4         107  xbababa     3.60     
Published 15 original articles · won praise 14 · views 5719

Guess you like

Origin blog.csdn.net/qq_39204060/article/details/102544181