Previous registry Mysql

Asked

Viewed 1,126 times

1

Hello I am trying to see the previous record in relation to the current record. I am using the following Cód.

SELECT * FROM Tabela WHERE compoid <"Contraband" ORDER BY compoid DESC LIMIT 1;

But it is returning the blank table What’s wrong? How can I search for the previous record using select in mysql

  • What should be the value of the previous record in compoid? Tested with other values? Great chance the problem is in the data.

  • From what I understand your field compoid would be an integer numeric, usually auto increment, you’re comparing it to a string? compoid < "Contraband"

  • @Kaduamaral Could you please send a code with this <"contraband" string? Please

  • @Bacco my doubts would be to take the value of the previous record in relation to the last position of records. Therefore the limit 1

  • This part is clear to me, what I asked is what would be the value of the previous record. I also asked if you tested with other values. You are ordering by Compoid for want alphabetical order?

  • @Bacco Yes ordering by Compoid, but next there is no way I know which is last record, since I am running all this from the client side via queyri. Or has?

  • 1

    If I were you, I’d select them all first, just in order, to see what comes back. Then I would put the limit back, because the problem may be, as I said, in the data you are testing, and not in the query. Your query seems right, but since you didn’t put the table structure in the question, it may not and it may be that.

  • @Fabríciosimonealanamendes for me to send you a code, I need to know the modeling of your table. Edit your question informing the modeling of the table and some values, so we can help you...

Show 3 more comments

1 answer

1

tested and worked well below it takes the penultimate record from the table

SELECT max(compoid)-1, t.* FROM Tabela t ORDER BY compoid DESC LIMIT 1;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.