So here is a little code snipped. You have to know, that the first table/subselect is stored within the fromItem from your PlainSelect and the followings within the joins. You have to process both. Thats due to the parsing architecture JSqlParser uses.
Within JSqlParser you have two types of traversing the returned object hierarchy: 1. visitor pattern, 2. direct object hierarchy access. I implemented both within this little example.
EDIT: To find hierarchical all subqueries JSqlParser identifies, I included Type 3, which is kind of a hack, since I somehow missused the deparser.
public class SimpleSqlParser10 {
public static void main(String args[]) throws JSQLParserException {
String sql = "SELECT * FROM myTable, (select * from myTable2) as data1, (select b from myTable3) as data2";
Select select = (Select)CCJSqlParserUtil.parse(sql);
System.out.println(select.toString());
System.out.println("Type 1: Visitor processing");
select.getSelectBody().accept(new SelectVisitorAdapter(){
@Override
public void visit(PlainSelect plainSelect) {
plainSelect.getFromItem().accept(fromVisitor);
if (plainSelect.getJoins()!=null)
plainSelect.getJoins().forEach(join -> join.getRightItem().accept(fromVisitor));
}
});
System.out.println("Type 2: simple method calls");
processFromItem(((PlainSelect)select.getSelectBody()).getFromItem());
if (((PlainSelect)select.getSelectBody()).getJoins()!=null)
((PlainSelect)select.getSelectBody()).getJoins().forEach(join -> processFromItem(join.getRightItem()));
System.out.println("Type 3: hierarchically process all subselects");
select.getSelectBody().accept(new SelectDeParser() {
@Override
public void visit(SubSelect subSelect) {
System.out.println(" found subselect=" + subSelect.toString());
super.visit(subSelect); }
});
}
private final static FromItemVisitorAdapter fromVisitor = new FromItemVisitorAdapter() {
@Override
public void visit(SubSelect subSelect) {
System.out.println("subselect=" + subSelect);
}
@Override
public void visit(Table table) {
System.out.println("table=" + table);
}
} ;
private static void processFromItem(FromItem fromItem) {
System.out.println("fromItem=" + fromItem);
}
}
This one outputs
SELECT * FROM myTable, (SELECT * FROM myTable2) AS data1, (SELECT b FROM myTable3) AS data2
Type 1: Visitor processing
table=myTable
subselect=(SELECT * FROM myTable2) AS data1
subselect=(SELECT b FROM myTable3) AS data2
Type 2: simple method calls
fromItem=myTable
fromItem=(SELECT * FROM myTable2) AS data1
fromItem=(SELECT b FROM myTable3) AS data2
Type 3: hierarchically process all subselects
found subselect=(SELECT * FROM myTable2) AS data1
found subselect=(SELECT b FROM myTable3) AS data2