使用数据库中的数据进行Ajax实时搜索

I've seen some (well-explained) examples where the available results of a live search are found in an xml file. But only a few (badly-explained) ones where the results come from a database. I'm interested in the second case.

One solution could be to prefetch the database data and put them in an xml object and the rest could work the same way. And this is something I can easily implement.

What I'm looking for is for another way, where the user types a letter (e.g. 'c') and the suggestions are generated on the spot (e.g. 'carrot', 'cabbage', 'celery', 'citrus'). Would this be achieved with a call to the database like

"SELECT name FROM vegetables WHERE name LIKE c%"?

And what if the user then presses another letter? (e.g. 'ca', so the recommended results should be 'carrot', 'cabbage'). Every key pressed should initiate a new SELECT? For some reason it doesn't sound like a good solution.

Is there a way to achieve this second method?

You can achieve this by sending ajax requests on every key up function in jquery. In response send back the results in json format so as to display them in a unordered list below the text field. On clicking of the li of this fill the textbox with the value in the text field.