MYSQL - 按首字母排序

Posted

技术标签:

【中文标题】MYSQL - 按首字母排序【英文标题】:MYSQL - Sort by first letter 【发布时间】:2012-02-28 15:53:32 【问题描述】:

我有这个 sql 查询(如下),它工作得很好,但我需要修改它以仅选择第一个字符为 $a 的记录。我尝试了 LIKE A% 的几种变体,但都没有成功。我的情况的症结似乎是名称别名。如果我在 ORDER 之前使用 WHERE name LIKE 'A%',我会收到错误消息。这似乎是合乎逻辑的地方。有什么建议吗?

SELECT  
  IF(company_name <> '', company_name, PC_last_name) AS name, 
  customer_id AS id, 
  company_name AS cn, 
  PC_first_name AS pcf, 
  PC_last_name AS pcl, 
  primary_phone 
FROM sales_customer 
ORDER BY name

【问题讨论】:

【参考方案1】:

试试这个,它会得到name的第一个字母。

SELECT IF(company_name <> '', company_name, PC_last_name) AS name, customer_id AS id, company_name AS cn, PC_first_name AS pcf, PC_last_name AS pcl, primary_phone
FROM sales_customer
ORDER BY SUBSTRING(name, 1, 1) ASC

【讨论】:

这返回了带有某种随机排序的整个表。我再次认为别名是问题所在。【参考方案2】:

我认为您不能在WHERE 上使用别名进行此比较。试试这个:

SELECT  
  IF(company_name <> '', company_name, PC_last_name) AS name, 
  customer_id AS id, 
  company_name AS cn, 
  PC_first_name AS pcf, 
  PC_last_name AS pcl, 
  primary_phone 
FROM sales_customer 
WHERE IF(company_name <> '', company_name, PC_last_name) LIKE 'A%'
ORDER BY name

【讨论】:

宾果游戏!工作完美。我知道它与别名有关,有什么特殊原因导致它不能与别名一起正常工作吗? 来自 mysql 手册,apud this answer:不允许在 WHERE 子句中引用列别名,因为列值可能尚未确定WHERE 子句被执行。见见Section B.1.5.4, “Problems with Column Aliases”。 感谢您提供相关信息!现在我知道了。【参考方案3】:

我在 dba stackexchange 上找到了这个: https://dba.stackexchange.com/questions/60137/mysql-is-it-possible-to-order-a-query-by-a-specific-letter-using-order-by

DROP TABLE IF EXISTS products;

create table products(pname CHAR(30),pdescription CHAR(30),price 
DECIMAL(10,2),manufacturer CHAR(30));

INSERT INTO products VALUES
('Toys','These are toys',15.25,'ABC'),
('Dolls','These are Dolls',35.25,'PQR'),
('DustPan','These are DustPan',75.25,'AZD'),
('Doors','These are Doors',175.25,'RAZD'),
('TV','These are TV',11175.25,'RAZD'),
('Bed','These are Bed',1175.25,'ARAZD');

/** Check all data **/

SELECT * FROM products;
+---------+-------------------+----------+--------------+
| pname   | pdescription      | price    | manufacturer |
+---------+-------------------+----------+--------------+
| Toys    | These are toys    |    15.25 | ABC          |
| Dolls   | These are Dolls   |    35.25 | PQR          |
| DustPan | These are DustPan |    75.25 | AZD          |
| Doors   | These are Doors   |   175.25 | RAZD         |
| TV      | These are TV      | 11175.25 | RAZD         |
| Bed     | These are Bed     |  1175.25 | ARAZD        |
+---------+-------------------+----------+--------------+
6 rows in set (0.00 sec)

/** Order by D% **/
SELECT 
        pname, pdescription, price
    FROM
    products
ORDER BY 
CASE
    WHEN pname LIKE 'D%' THEN 1
    ELSE 2
END;
+---------+-------------------+----------+
 | pname   | pdescription      | price    |
+---------+-------------------+----------+
| Dolls   | These are Dolls   |    35.25 |
| DustPan | These are DustPan |    75.25 |
| Doors   | These are Doors   |   175.25 |
| Toys    | These are toys    |    15.25 |
| TV      | These are TV      | 11175.25 |
| Bed     | These are Bed     |  1175.25 |
+---------+-------------------+----------+
6 rows in set (0.00 sec)

【讨论】:

以上是关于MYSQL - 按首字母排序的主要内容,如果未能解决你的问题,请参考以下文章

mysql根据汉字首字母排序的方法并按字母分组

按首字母分组排序数组

如何设置排序器强度和分解以按首字母排序 unicode 字符串

按首字母 Javascript 按字母顺序对对象进行排序和分组

JAVA通过数组按首字母排序怎么做?

Drupal:如何在视图中按首字母排序和显示节点分组