mysql - 在sql中的表中用不同的字符串替换字符串

标签 mysql replace xampp substitution

大家下午好。

我正在尝试替换 sql 中特定列中的查询结果。表格如下:

+-----------+----------+-------+-------+---------+
| firstName | lastName | major | minor | credits |
+-----------+----------+-------+-------+---------+
| Owen      | McCarthy | Math  | CSCI  |       0 |
| Mary      | Jones    | Math  | CSCI  |      42 |
+-----------+----------+-------+-------+---------+

我需要将 Math 改为 Mathematics,将 CSCI 改为 Computer Science。有没有我可以替换它的特定代码?

我用来生成这个表的代码是这样的:

select firstName, lastname, major, minor, credits from student where major = 'Math' and minor = 'CSCI'; 

感谢任何帮助,谢谢,祝你星期天 super 碗愉快

最佳答案

执行此操作的最佳方法是使用具有连接的查找表。这样,您可以确保所有替换都使用相同的值。

不过,在单个查询中,您可以使用 case 表达式。但是,在您的查询中,由于 where 子句,这甚至不是必需的。只需使用常量:

select firstName, lastname,
       'Mathematics' as major, 'Computer Science' as minor, credits
from student
where major = 'Math' and minor = 'CSCI'; 

如果没有 where 子句,您会:

select firstName, lastname,
       (case when major = 'Math' then 'Mathematics' else major end) as major,
       (case when minor = 'CSCI' then 'Computer Science' else minor end) as minor,
       credits
from student;

关于mysql - 在sql中的表中用不同的字符串替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42056590/

相关文章:

php - htaccess/url 使用 Silex、XAMPP 和多文件夹结构重写

java - 无法在 XAMPP 中编辑 Tomcat Java 选项

mysql - 来自子查询的两列(mysql)

r - 替换 tibble 列表中值的更简洁方法

powershell - 替换powershell中变量的一部分而不替换其余部分?

vim - VIM:如何替换单词

mysql - 如何为 MySQL JOIN 创建正确的语句?

MySQL LIMIT 关键字优化问题

mysql - 高效且可维护的查询,用于比较一段时间内的派生数据

linux - Windows 机器中的 UNIX/LINUX 本地主机服务器