我可以写一个基于属性值的SQL语句吗? [英] Can I write one SQL statement based on a property value?
问题描述
我想知道我是否可以编写一个SQL语句,或者如果我将使用应用程序的控制流编写许多不同的SQL语句。
I was wondering if I could write one SQL statement or if I will have to write many different SQL statements using the control flow of the application.
基本上,我有一个名为学生的表,将有一个列性(M,F)。
Essentially, I have a table called Students that will have a column sex(M,F).
我也有一个称为Divisions的表,它将有一个名为sex(null,M,F)的列。如果值为null,我要检索所有学生,包括男性和女性。
I also have a table called Divisions that will have a column named sex(null, M, F). If the value is null, I want to retrieve all students, both male and female.
我知道我可以写两个不同的语句来完成这项工作。但是,我有其他列将工作方式相同,我想知道是否可以写一个语句,而不是10。
I know I can write two different statements to get this job done. But, I have other columns that will work the same way and I was wondering if there is a way I could write one statement rather then 10.
推荐答案
是的,只要确保您的连接条件满足您的要求:
这样的东西(未测试)
Yes, just make sure your join condition does what you ask: Something like this (untested)
select * from students s left outer join division d on (s.sex=d.sex or d.sex is null)
这篇关于我可以写一个基于属性值的SQL语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!