我正面临着通过某些(子)字符串搜索实体的简单问题,它们可能包含这些字符串。例如。我的用户使用u_name作为" rags"," mechrags"," meragsch"我将进入搜索窗口" rags"和我 使用以下查询来搜索包含" rags"
的字符串 SELECT u FROM User_personal u WHERE u_name LIKE'%rags%'
我想根据首次出现的搜索字符串" rags"来排序上述查询返回的结果。在字符串中,结果将是rags,meragsch,mechrags(按此顺序)。
我正在考虑将LOCATE函数用于上述结果,如下所示
SELECT u FROM User_personal u WHERE u_name LIKE'%rags%' order by
LOCATE(u.u_name,'rags')
但结果比前一个好,但是 如果有4个用户" ragsch"," rags"," meragsch"," mechrags"答案
应该是" rags"," ragsch"," meragsch"," mechrags"但是输出是
" ragsch""抹布"" meragsch"" mechrags"即数据库顺序
我正在考虑使用我们首先订购长度的子查询,然后在第一次出现" rags"结果可能会出现,但查询未运行
select user from User_personal user where exists (select u from User_personal u where u.u_name like'%rags%' order by LENGTH(u.u_name)) order by LOCATE(user.u_name,'rags')
AS HERE http://docs.oracle.com/html/E13946_04/ejb3_langref.html#ejb3_langref_subquerie
有人能建议一种获得正确输出的方法吗?
SELECT u FROM User_personal u WHERE u_name LIKE'%rags%' ORDER BY LOCATE(u.u_name,'rags'), LENGTH(u.u_name)
给出了OBJECT_DB建议的正确输出
现在我在下面的方法中使用它有问题
public static List<User_personal> search(String str, EntityManager em)
{
String dup ="%"+str+"%";
Query q = ("SELECT u FROM User_personal u WHERE u_name LIKE :dup ORDER
BY LOCATE(u.u_name,'rags'), LENGTH(u.u_name)",User_personal.class)
List<User_personal> result = q.getResultList();
if (!result.isEmpty()) {
return result;
}
return null;}*
如果我把str作为&#34; rags&#34;我应该得到所有&#34;你&#34;具有所需条件,但它显示如下错误
<h1>HTTP Status 500 - Attempt to execute a query with too few arguments
为了检查我给了&#34; rags&#34;直接获得所需的输出,但无法理解传递参数的问题。
public static List<User_personal> search(String str,EntityManager em)
{
//String dup ="%"+str+"%";
Query q = ("SELECT u FROM User_personal u WHERE u_name LIKE '%rags%'
ORDER BY LOCATE(u.u_name,'rags'), LENGTH(u.u_name)",User_personal.class)
List<User_personal> result = q.getResultList();
if (!result.isEmpty()) {
return result;
}
return null;}*
给出正确的输出。
任何人都可以建议在这里使用变量的正确方法
得到它!!!!!!
public static List<User_personal> search(String str, EntityManager em) {
Query q = em.createQuery("SELECT u FROM User_personal u WHERE
CONCAT(u.fname,u.lname) LIKE :name ORDER BY
LOCATE(CONCAT(u.fname,u.lname),'rags'),
LENGTH(CONCAT(u.fname,u.lname))",
User_personal.class);
q.setParameter("name", "%"+str+"%");
List<User_personal> result = q.getResultList();
if (!result.isEmpty()) {
return result;
}
return null;}
这给了所有&#34; u&#34;用&#34; rags&#34;并且还按照长度和第一次出现的顺序排列&#34; rags&#34;
所以我们可以这样做,就像非案例敏感一样,如#34; Rags&#34;或&#34; RAGS&#34;或者&#34;破布&#34;应该返回相同的值
DONE !!!
public static List<User_personal> search(String str, EntityManager em) {
Query q = em.createQuery("SELECT u FROM User_personal u WHERE
lower(CONCAT(u.fname,u.lname)) LIKE lower(:name) ORDER BY
LOCATE(CONCAT(u.fname,u.lname),'rags'),
LENGTH(CONCAT(u.fname,u.lname))",
User_personal.class);
//SELECT u FROM User u WHERE lower(u.username) LIKE :username
q.setParameter("name", "%"+str+"%");
List<User_personal> result = q.getResultList();
if (!result.isEmpty()) {
return result;
}
return null;}
如果我给#34; Rags&#34;它找到&#34; rags&#34;和&#34; Rags&#34;和&#34; RAGS&#34;这就是我需要的东西:))
谢天谢地接受任何更好的方法尝试更好的方法 谢谢你们所有人
答案 0 :(得分:0)
也许只是2个订单表达式?
SELECT u FROM User_personal u WHERE u_name LIKE&#39;%rags%&#39; ORDER BY LOCATE(u.u_name,&#39; rags&#39;),LENGTH(u.u_name)