Use Java to understand SQL syntax, and use Java to understand SQL syntax

Source: Internet
Author: User

Use Java to understand SQL syntax, and use Java to understand SQL syntax

Select is equivalent to for Loop
Select id from IDArray

LinkedList a = new LinkedList();for ( int i=0 ; i<tableA.length ; i++){    a.add(IDArray.get("id" ) );}return a;

When executing a subquery, it can be understood
Select id, (select name from nameArray) as names, from Idarray

LinkedList a = new LinkedList();for ( int i=0 ; i<tableA.length ; i++){    a.add(IDArray.get("id" ) );    for(int j=0 ; j<nameArray.length ; j++){       a.add(namesArray.get("name"));     }}

It is obvious that there is a one-to-many relationship. So the execution is incorrect. SQL error reported

Therefore, in select, you must determine a specific value.
Select id, (select name from nameArray where nameId = id) as names from Idarray

LinkedList a = new LinkedList();for ( int i=0 ; i<tableA.length ; i++){   Object object =  IDArray.get("id" ) ;  for ( int j=0 ; j<nameArray.length ; i++){     if ( nameArray.get("nameId")==object){       Object object2 = nameArray.get("nameId");       a.add(object2);      }}       a.add(object);  }

When and only when the value of the if condition is a single value, it corresponds

What about groupBy?

Select id, (select name from nameArray where nameId = id) as names from Idarray group by id

LinkedList a = new LinkedList();for ( int i=0 ; i<tableA.length ; i++){   Object object =  IDArray.get("id" ) ;  for ( int j=0 ; j<nameArray.length ; i++){     if ( nameArray.get("nameId")==object){       Object object2 = nameArray.get("nameId");       a.add(object2);      }}       a.add(object);  }LinkedList groupByArray = new LinkedList();for ( int i=0 ; i< a. length  ; i++){      int id = a[i].getId();     LinkedList tempArray = new LinkedList();     for ( int j=0 ; j<j.length ; j++){       int temp = a[j].getId();      if ( id==temp){         tempArray.add(temp);        }    }   groupByArray.add(tempArray);}

Join the study exchange group 569772982 to learn and exchange.

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.