import grails.util.Holders import org.hibernate.SessionFactory import org.apache.commons.lang.StringUtils def dataSource SessionFactory sessionFactory String connectionURL = dataSource.targetDataSource.targetDataSource.poolProperties.url connectionURL = StringUtils.substringAfterLast(connectionURL, '/') connectionURL = StringUtils.substringBefore(connectionURL, '?') println(connectionURL) String databaseName = sessionFactory.currentSession.createSQLQuery("SELECT DATABASE()") .setReadOnly(true).setCacheable(false).list().first() println(databaseName) String query = "SELECT table_name FROM information_schema.tables WHERE table_schema='$databaseName'".toString() List list = sessionFactory.currentSession.createSQLQuery(query) .setReadOnly(true).setCacheable(false).list() println(list.size()) println(list)
Its so simple. Just need to do below thins: |
SELECT SUBSTRING_INDEX(GROUP_CONCAT(x.id ORDER BY x.nx DESC), ',', 2) as row_name from some_table GROUP BY some_field |
It will select First two values only. |
It total value of GROUP_CONCAT is "1,2,3,4,5" Then Using SUBSTRING_INDEX would be like "1,2" |
You can use DISTINCT in GROUP_CONCAT function like SUBSTRING_INDEX(DISTINCT(GROUP_CONCAT(x.id ORDER BY x.nx DESC)), ',', 2) |