2
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+"%'";
4

3 回答 3

4

也许你的意思是这个?

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+ "%'" ;

但我建议你PreparedStatements在查询上使用来避免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();
于 2013-02-13T11:27:12.570 回答
1

尝试这个:

 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+"%'";
于 2013-02-13T11:27:10.910 回答
0

有一些小的改动,请查看。

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+"%\'";

试试这个,告诉我它是否有效。

于 2013-02-15T10:22:00.647 回答