如何获得所需的输出?

时间:2013-10-09 04:33:34

标签: mysql sql

编写一个SELECT语句,从Vendors表中返回这些列:

  • vendor_name列
  • 所有大写字母的vendor_name列
  • vendor_phone列
  • 每个电话号码的最后四位数

如果能够正常工作,请将后面的列添加到结果集中。这更加困难,因为这些列需要在函数内使用函数。

  • 每个供应商名称中的第二个字,如果有的话;否则,空白
  • vendor_phone列,其中部分数字用点分隔,如555.555.5555

这是我到目前为止所尝试的:

select vendor_name, UPPER(vendor_name) AS VENDOR_NAME_UPPER,
vendor_phone, SUBSTR(vendor_phone, 11, 4), 
Replace
(replace
(replace(vendor_phone, ') ', '.')
, '(', '')
, '-','.') AS vendor_phone_dot,
SUBSTR(vendor_name, (INSTR(vendor_name, ' ') + 1)) AS Second_Word
from vendors;

2 个答案:

答案 0 :(得分:0)

试试这个..

SELECT vendor_name, CONVERT(VARCHAR(50), UPPER(vendor_name)),SUBSTRING(vendor_phone,  LEN(vendor_phone)-3 ,4) FROM Vendors 

答案 1 :(得分:0)

SELECT vendor_name,
UPPER(vendor_name),
vendor_phone,
RIGHT(vendor_phone, 4),
REPLACE(REPLACE(REPLACE(RIGHT(vendor_phone, 13),')','.')," ",""),"-","."),
CASE vendor_name
    WHEN (LOCATE(' ', vendor_name) = 0) THEN (SUBSTRING_INDEX(SUBSTRING(vendor_name, LOCATE(' ', vendor_name) + 1),' ', 1))
    ELSE ''
END
FROM ap.vendors;

Murach的MySQL(第3版),第9章,练习3