Within MySQL Query connection

Test data:
the Suppliers

CREATE TABLE suppliers(
s_id INT NOT NULL AUTO_INCREMENT,
s_name CHAR(50) NOT NULL,
s_city CHAR(10) NULL,
s_zip CHAR(10) NULL,
s_call CHAR(50) NOT NULL,
PRIMARY KEY(s_id)
);
INSERT INTO suppliers(s_id, s_name,s_city,  s_zip, s_call)
VALUES(101,'FastFruit Inc.','Tianjin','300000','48075'),
(NULL,'LT Supplies','Chongqing','400000','44333'),
(NULL,'ACME','Shanghai','200000','90046'),
(NULL,'FNK Inc.','Zhongshan','528437','11111'),
(NULL,'Good Set','Taiyuang','030000', '22222'),
(NULL,'Just Eat Ours','Beijing','010', '45678'),
(NULL,'DK Inc.','Zhengzhou','450000', '33332');

fruits

CREATE TABLE fruits
(
f_id    char(10)     	NOT NULL,
s_id    INT        	NOT NULL,
f_name  char(255)  	NOT NULL,
f_price decimal(8,2)  	NOT NULL,
PRIMARY KEY(f_id) 
); 
 
 
 INSERT INTO fruits (f_id, s_id, f_name, f_price)
     VALUES('a1', 101,'apple',5.2),
     ('b1',101,'blackberry', 10.2),
     ('bs1',102,'orange', 11.2),
     ('bs2',105,'melon',8.2),
     ('t1',102,'banana', 10.3),
     ('t2',102,'grape', 5.3),
     ('o2',103,'coconut', 9.2),
     ('c0',101,'cherry', 3.2),
     ('a2',103, 'apricot',2.2),
     ('l2',104,'lemon', 6.4),
     ('b2',104,'berry', 7.6),
     ('m1',106,'mango', 15.6),
     ('m2',105,'xbabay', 2.6),
     ('t4',107,'xbababa', 3.6),
     ('m3',105,'xxtt', 11.6),
     ('b5',107,'xxxx', 3.6);

Table Structure:
Fruit

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     

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  
  • Inner join query
SELECT suppliers.`s_id`,s_name,f_name,f_price
FROM fruits,suppliers
WHERE fruits.`s_id`=suppliers.`s_id`;

Table as fruits and suppliers Table s_id have the same field, so when the comparison, the table needs to fully qualified name (the format of "table name, column name"), is given only if the s_id, MySQL will not know which refers to a, and returns an error message.

- inner join

SELECT suppliers.`s_id`,s_name,f_name,f_price
FROM fruits inner join suppliers
on fruits.`s_id`=suppliers.`s_id`;

Query supply f_id = 'a1' fruit-supplied types of fruit and fruit vendor's name

select f.`f_name`,s.`s_name` 
from fruits as f inner join suppliers as s on f.`s_id`=s.`s_id` 
where f.`s_id`=(select s_id from fruits as f where f.`f_id`='a1')

inner join XXXX on XXXX where XXXX

- self-join
query supply f_id = 'a1' of the type provided by Fruit Fruit

select  f.`f_name` from fruits as f , fruits as r  
where f.`s_id`=r.s_id and r.f_id='a1';

Note: select sub-query

SELECT f.`f_name`
 FROM fruits AS f  
 WHERE f.`s_id`=(SELECT s_id FROM fruits AS f WHERE f.`f_id`='a1')
Published 15 original articles · won praise 14 · views 5720

Guess you like

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