大家下午好。
我正在尝试替换 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/