MYSQL-按第一个字母排序
我有一个sql查询(如下),它工作得很好,但我需要修改它,只选择第一个字符为$的记录。我尝试了一些类似于%的变体,但运气不好。我的处境的关键似乎是名字别名。如果我在订单前使用WHERE名称,比如“A%”,我会得到一个错误。这似乎是一个合乎逻辑的地方。有什么建议吗MYSQL-按第一个字母排序,mysql,Mysql,我有一个sql查询(如下),它工作得很好,但我需要修改它,只选择第一个字符为$的记录。我尝试了一些类似于%的变体,但运气不好。我的处境的关键似乎是名字别名。如果我在订单前使用WHERE名称,比如“A%”,我会得到一个错误。这似乎是一个合乎逻辑的地方。有什么建议吗 SELECT IF(company_name <> '', company_name, PC_last_name) AS name, customer_id AS id, company_name AS
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
选择
如果(公司名称“”、公司名称、PC姓氏)作为名称,
客户id作为id,
公司名称为cn,
PC_第一个名称为pcf,
PCU姓氏为pcl,
主电话
来自销售和客户
点名
试试这个,它是名称的第一个字母
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
选择(公司名称“”、公司名称、个人电脑姓氏)作为姓名、客户id作为id、公司名称作为cn、个人电脑姓氏作为pcf、个人电脑姓氏作为pcl、主手机
来自销售和客户
按子字符串(名称,1,1)排序ASC
我认为您不能在的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
选择
如果(公司名称“”、公司名称、PC姓氏)作为名称,
客户id作为id,
公司名称为cn,
PC_第一个名称为pcf,
PCU姓氏为pcl,
主电话
来自销售和客户
其中,如果(公司名称“”、公司名称、PC姓氏)像“A%”
点名
我在dba stackexchange上找到了这个:
答对 了工作得很好。我知道这与别名有关。有没有任何特定的原因说明它不能正确使用别名?从MySQL手册中,apud:不允许在WHERE子句中引用列别名,因为在执行WHERE子句时,列值可能尚未确定。谢谢你提供的信息!现在我知道了。它以某种随机排序返回了整张桌子。我再次认为别名是问题所在。
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)