"MySQL" statement optimization

Source: Internet
Author: User

On the forum saw a similar split to view the results of the stem for a moment preface to cure

Like a B watch.
Table A
Id,name
1,a
2,b
3,c

B table
Fk_id,type,value
1,socer,100
1,socker,90
2,socker,20

To find such a result
A.id,a.name,a.socker
1, A, ' 100,90 '
2,b, ' 20 '


That's when the select was made a field socker the column values of table B into a row, using the "," symbol to split. Request SQL Statement Thanks

Handwritten tablesCreate TableA (IDint(Ten) not NULLauto_increment, NAMEvarchar( -),   Primary Key(ID));Create TableB (fk_idint(Ten) not NULLauto_increment, TYPEvarchar(Ten),
VALUEvarchar (10)
);

Implement SQL as follows ①

SELECT   a.id,        a.name,        as  sockerfrom    A        INNER  JOINon=  b.fk_idGROUP  by a.id,        A . NAME

Implement SQL as follows ② support output write into hard drive path

SELECTa.ID as  'a.id', A.name as   'A.name', Group_concat (b.value) as  'A.socker'  fromA, bWHEREa.ID=b.fk_idGROUP  bya.id, A.name intoOUTFILE "D:/NEWS. SQL ";

"MySQL" statement optimization

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.