This builder is stateful and therefore should only be used by one thread at a time. However, once a query has been built, the builder can be {@link #clear() cleared} and used to create another query.
The order in which the methods are called are (for the most part) important. Simply call the methods in the same order that would be most natural in a normal SQL query. For example, the following code creates a Query object that is equivalent to " SELECT * FROM table
":
QueryCommand query = builder.selectStar().from("table").query();
Here are a few other examples:
SQL Statement | QueryBuilder code |
---|---|
SELECT * FROM table1 INNER JOIN table2 ON table2.c0 = table1.c0 | query = builder.selectStar().from("table1").join("table2").on("table2.c0=table1.c0").query(); |
SELECT * FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.c0 = t2.c0 | query = builder.selectStar().from("table1 AS t1").join("table2 AS t2").on("t1.c0=t2.c0").query(); |
SELECT * FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.c0 = t2.c0 INNER JOIN table3 AS t3 ON t1.c1 = t3.c1 | query = builder.selectStar() .from("table1 AS t1") .innerJoin("table2 AS t2") .on("t1.c0=t2.c0") .innerJoin("table3 AS t3") .on("t1.c1=t3.c1") .query(); |
SELECT * FROM table1 UNION SELECT * FROM table2 | query = builder.selectStar().from("table1").union().selectStar().from("table2").query(); |
SELECT t1.c1,t1.c2,t2.c3 FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.c0 = t2.c0 UNION ALL SELECT t3.c1,t3.c2,t4.c3 FROM table3 AS t3 INNER JOIN table4 AS t4 ON t3.c0 = t4.c0 | query = builder.select("t1.c1","t1.c2","t2.c3",) .from("table1 AS t1") .innerJoin("table2 AS t2") .on("t1.c0=t2.c0") .union() .select("t3.c1","t3.c2","t4.c3",) .from("table3 AS t3") .innerJoin("table4 AS t4") .on("t3.c0=t4.c0") .query(); |
|
|