简体   繁体   中英

how to concat sql query using union operator

String Query="select * from recommendation.sports WHERE feed LIKE'%"+s1+"%'

    UNION "select * from recommendation.software  WHERE feed LIKE '%"+s1+"%'

    UNION "select * from recommendation.website   WHERE feed LIKE '%"+s1+"%'

    UNION "select * from recommendation.others    WHERE feed LIKE '%"+s1+"%'

    UNION "select * from recommendation.business  WHERE feed LIKE '%"+s1+"%'";

maybe you mean this?

String Query = "select * from recommendation.sports WHERE feed LIKE '%" + s1 + "%' " +
                "UNION " +
                "select * from recommendation.software  WHERE feed LIKE '%" +s1+ "%' " +
                "UNION " +
                "select * from recommendation.website   WHERE feed LIKE '%" +s1+" %' " +
                "UNION " +
                "select * from recommendation.others    WHERE feed LIKE '%"+s1+"%' " +
                "UNION " +
                "select * from recommendation.business  WHERE feed LIKE '%" +s1+ "%'" ;

but I advise you to use PreparedStatements on the query to avoid SQL Injection .

String Query = "select * from recommendation.sports WHERE feed LIKE CONCAT('%', ?, '%') " +
                "UNION " +
                "select * from recommendation.software  WHERE feed LIKE CONCAT('%', ?, '%') " +
                "UNION " +
                "select * from recommendation.website   WHERE feed LIKE CONCAT('%', ?, '%') " +
                "UNION " +
                "select * from recommendation.others    WHERE feed LIKE CONCAT('%', ?, '%')  " +
                "UNION " +
                "select * from recommendation.business  WHERE feed LIKE CONCAT('%', ?, '%')" ;

PreparedStatement pstmt = con.prepareStatement(Query);
pstmt.setString(1, s1);
pstmt.setString(2, s1);
pstmt.setString(3, s1);
pstmt.setString(4, s1);
pstmt.setString(5, s1);
ResultSet _result = pstmt.executeQuery();

Try This:

 String Query="select * from recommendation.sports WHERE feed LIKE'%"+s1+"%'

    UNION select * from recommendation.software  WHERE feed LIKE '%"+s1+"%'

    UNION select * from recommendation.website   WHERE feed LIKE '%"+s1+"%'

    UNION select * from recommendation.others    WHERE feed LIKE '%"+s1+"%'

    UNION select * from recommendation.business  WHERE feed LIKE '%"+s1+"%'";

There are some minor changes, please review it.

String Query="select * from recommendation.sports WHERE feed LIKE \'%"+s1+"%\'

    UNION select * from recommendation.software  WHERE feed LIKE \'%"+s1+"%\'

    UNION select * from recommendation.website   WHERE feed LIKE \'%"+s1+"%\'

    UNION select * from recommendation.others    WHERE feed LIKE \'%"+s1+"%\'

    UNION select * from recommendation.business  WHERE feed LIKE \'%"+s1+"%\'";

Try this and tell me if it works or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM