SQL Query with MySQL

前端 未结 3 1163
时光取名叫无心
时光取名叫无心 2020-12-20 01:12

I am implementing a Restful Web Service in java (JAX-RS) using Jesey. I run it on Tomcat v7.0 I use Hibernate to map the data to the database (MySQL). I have a query to fetc

相关标签:
3条回答
  • 2020-12-20 01:51

    mysql doesn't have TOP, use the ORDER BY and LIMIT clauses at the end of the sql.

    0 讨论(0)
  • 2020-12-20 02:02

    I am not sure TOP function is available in mysql but try to use it and share the result. you can write like this: SELECT TOP 2 * FROM Persons

    check this link, it will furnish you with more details. enter link description here

    0 讨论(0)
  • 2020-12-20 02:11

    I think you are asking for the top 3 Deliverables from Task ordered by ID. You could try something like this:

    Edit: Ok I'll take one more stab at this. This should give you the top 3 Deliverables ordered by Task.id taking only the Deliverable associated to the max(Task.id)

    deliverables = 
            (List<Deliverable>) session.createQuery(
                "from Deliverable as d, Task as t
                    where t.deliverable.id = d.id and
                    t.id = (select max(t1.id) from Task t1 where t1.deliverable.id = d.id)
                    and d.project.id= :id
                    and t.user.username = :name
                    order by t.id desc")
            .setMaxResults(3)
            .setLong("id", projectId)
            .setString("name", username)
            .list();
    
    0 讨论(0)
提交回复
热议问题