duanfa0072 2015-06-25 17:28
浏览 7

Mysql,从字段值和id值中选择行

Here's the table:

  id  |  name
------+---------
  10  |  val1
   3  |  val1
   5  |  val1
   4  |  val2
   1  |  val2
   6  |  val3
   8  |  val3
   7  |  val3
   9  |  val3
   2  |  val4

The table is ordered by value column and I've to preserve this order

How can get all the rows after id=8 and name>val3 with this result?

  id  |  name
------+---------
   8  |  val3
   7  |  val3
   9  |  val3
   2  |  val4

I've tried with query like:

SELECT FORM table WHERE id>8 AND VALUE name>'val3' 

but obviously is not the result i looking for... any idea?

Edit 1:

Sorry but it isn't possible change the design of the db, and let me say that I think it's hard to say how a db is designed from a general table used to explain only a specific problem. However, yes i know the query is wrong but I need all the rows after value of the second column and FROM a certain id value... it's a bit more clear?

Edit 2:

When I say FROM a certain id value, i'm not talking about cardinal order of the id column, but i'm talking about the position after that with the id value specified

Edit 3:

Ok... I've a table ordered by name column, how can I select only last n rows? I can use eventually an id value (8 in this case) and a name value (val3 in this case)... i know it's unusual...

Edit 4:

Ok i've rewrite the question in a better way here this question is not usefull anymore please close it, thankyou for the time you've spent anyway

  • 写回答

3条回答 默认 最新

  • doupingpeng7567 2015-06-25 17:42
    关注

    @mikeb in the comments is correct. Consider changing the design of your database.

    If that's not an option and you only have this data. Plus your sample data is incorrect too. You want the value with id = 2 and id = 7 in your output however you say you want id>=8 (you said id>8 but I wonder why you wrote the row with id=9 in the sample output).

    If you only want

    How can get all the rows after id>=8 and value>val3 having with this result?

    you can do something like this

    SELECT id, name
    FROM table 
    WHERE  id>=8 AND  name IN ('val3','val4') 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题