Use of any product of the supplier in Mysql Query

Source: Internet
Author: User

The following articles mainly describe the solutions used by all the suppliers in Mysql query and any of their own products. This article mainly describes the specific use solutions of Group, according to a user's demand, the main cause is as follows:

There is a supplier Table a, product table B (hereinafter referred to as a, B, a and B) is a one-to-many relationship (one supplier corresponds to multiple items), and now I want to extract one-to-one, that is to say, all the suppliers are extracted, but each supplier can extract only one item record to match.

The test code I provided is as follows:

 
 
  1. View plaincopy to clipboardprint?
  2. Create table 't_ supplier '(
  3. 'Id' int (11) not null,
  4. 'Name' varchar (50) not null,
  5. Primary key ('id ')
  6. ) ENGINE = InnoDB default charset = utf8;
  7. Create table 't_ goods '(
  8. 'Id' int (11) not null AUTO_INCREMENT,
  9. 'Supplier _ id' int (11) not null,
  10. 'Name' varchar (50) default null,
  11. Primary key ('id '),
  12. KEY 'Supplier _ id' ('Supplier _ id '),
  13. CONSTRAINT 't_ goods_ibfk_1 'foreign key ('Supplier _ id ')
    REFERENCES't _ supplier '('id ')
  14. ) ENGINE = InnoDB AUTO_INCREMENT = 5 default charset = utf8;
  15. Insert into t_Supplier values (1, 'tianjin supplier '), (2, 'Beijing supplier ');
  16. Insert into t_Goods values (1, 1, 'tianjin product 1 '),
    (2, 1, 'tianjin product 2'), (3, 2, 'Beijing product 1'), (4, 2, 'Beijing product 2 ');

Mysql query statement. Pay attention to the group usage.

 
 
  1. select * from t_supplier s   
  2. left join t_goods g on g.supplier_id=s.id   
  3. group by s.id  

All the suppliers of Mysql query results and one of their products

 
 
  1. Create table 't_ supplier '(
  2. 'Id' int (11) not null,
  3. 'Name' varchar (50) not null,
  4. Primary key ('id ')
  5. ) ENGINE = InnoDB default charset = utf8;
  6. Create table 't_ goods '(
  7. 'Id' int (11) not null AUTO_INCREMENT,
  8. 'Supplier _ id' int (11) not null,
  9. 'Name' varchar (50) default null,
  10. Primary key ('id '),
  11. KEY 'Supplier _ id' ('Supplier _ id '),
  12. CONSTRAINT 't_ goods_ibfk_1 'foreign key ('Supplier _ id') REFERENCES 't_ Supplier' ('id ')
  13. ) ENGINE = InnoDB AUTO_INCREMENT = 5 default charset = utf8;
  14. Insert into t_Supplier values (1, 'tianjin supplier '), (2, 'Beijing supplier ');
  15. Insert into t_Goods values (, 'tianjin product 1'), (, 'tianjin product 2'), (, 'Beijing product 1, 'Beijing product 2 ');

Query statement. Pay attention to the group usage.

 
 
  1. select * from t_supplier s  
  2. left join t_goods g on g.supplier_id=s.id  
  3. group by s.id 

Mysql query results, all vendors, and one of their products in Mysql, non-Group and aggregate fields can appear in select, and the system will automatically select a data. This is not allowed in other databases.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.