有人可以提供帮助,我需要从所有字段值为%someString%的字段中获取所有值。我的表有17个字段,我不想在查询中写入所有字段。 DB Mysql。
select * from Deputes_APPEAL where filePath, checkForPlagiarism, contentOfResolution, countOfPages, dateForCheck, incomeNumber, incomingDate, mainWorker, nameAndAddressOfApplicant, nameOfDepute, outDate, outNumber, result, secondaryWorkers, shortContentOfAppeal, statusOfWorking, themeOfAppeal, TypeOfDeputeAppeal, unitWhoDoResolution like %someString%
答案 0 :(得分:0)
您可以通过以下方式实现这一目标:
SELECT
CONCAT('SELECT * FROM Deputes_APPEAL WHERE ',GROUP_CONCAT(COLUMN_NAME SEPARATOR ' LIKE \'%something%\' OR '),' LIKE \'%something%\'')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'YOUR_DATABASE_NAME'
AND TABLE_NAME = 'YOUR_TABLE_NAME'
AND COLUMN_NAME NOT IN ('Column1','Column2',....);
注意:您需要将列名放在NOT IN
子句中,以便在LIKE
表达式中省略。
输出:通过运行上述查询,您将获得如下输出:
SELECT * FROM Deputes_APPEAL WHERE column_name1 LIKE '%something%' OR .......
现在抓住这个查询并运行。
答案 1 :(得分:0)
使用循环构建SQL - 超级简单。
final String[] properties = new String[] { /* all your fields here */ };
// build the SQL
StringBuilder sb = new StringBuilder();
sb.append( "SELECT o FROM YourEntity o " );
if ( properties.length > 0 ) {
sb.append( "WHERE ( " ).append( properties[0] ).append( "LIKE :v0" );
for ( int i = 1; i < properties.length; ++i ) {
sb.append( " OR " ).append( properties[i] ).append( "LIKE :v" ).append(i);
}
sb.append( " ) " );
}
// build the query.
Query query = session.createQuery( sb.toString() );
for ( int i = 0; i < properties.length; ++i ) {
query.setParameter( "v" + i, "%something%" );
}