L
L
lidiya112018-09-09 20:41:09
MySQL
lidiya11, 2018-09-09 20:41:09

How to find row id by string value in MySQL?

I want to find row id in mySQL table by string value(java). That is, for example, I have columns id, name and age. And I want to find Anya named "Anya" and find out what her id is. And if there are several An? How can I find only the first or just some one (doesn't matter)? I added LIMIT 1. Here are some options that have been tried:

id = jdbcTemplate.query("SELECT * FROM people WHERE name LIKE '%{$"+Ann+"}%'", new ResultSetExtractor<Long>() {
            @Override
            public Long extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                return rs.next() ? rs.getLong("id") : null ;
            }
        });
id = jdbcTemplate.query("SELECT * FROM people WHERE name = 'Ann'", new ResultSetExtractor<Long>() {
            @Override
            public Long extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                return rs.next() ? rs.getLong("id") : null ;
            }
        });

TransientDataAccessResourceException errors popped up, the grammar was not correct, or the result was null.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
ponaehal, 2018-09-10
@ponaehal

SELECT TOP 1 id FROM people WHERE name = 'Ann'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question