doukun8944 2015-03-31 04:29
浏览 44

搜索列中每个空格分隔值的字符串

Suppose I have a column named CustomerNames in database with values:

  1. Vins et alcools Chevalier
  2. Magazzini Alimentari Riuniti
  3. Hungry Owl All-Night Grocers
  4. Split Rail Beer & Ale
  5. Alfreds Futterkiste

I want to make a query which will search only for the first letters of FIRSTNAME and LASTNAME, and list the names in ASC order based on FIRSTNAME then LASTNAME. for example if I search for "al" in above table, it should return me something like below:

  1. Alfreds Futterkiste
  2. Vins et alcools Chevalier
  3. Split Rail Beer & Ale
  4. Magazzini Alimentari Riuniti
  5. Hungry Owl All-Night Grocers

I have used below query.

SELECT * FROM Customers where CustomerName LIKE 'al%' OR CustomerName LIKE '% al%' ORDER BY CustomerName 

Output:

  1. Alfreds Futterkiste
  2. Hungry Owl All-Night Grocers
  3. Magazzini Alimentari Riuniti
  4. Split Rail Beer & Ale
  5. Vins et alcools Chevalier

Which is ordering results only on the basis of firstnames. But I want a query which gives output using firstnames and then lastnames. Thanks.

  • 写回答

2条回答 默认 最新

  • douchui1488 2015-03-31 05:23
    关注

    I dont think there's a straight forward answer to that. You need to write a function or store procedure to order results based on a column-values-that-is-space-separated

    Have a look at this MySQL: how to sort the words in a string using a stored function?

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效