DWQA QuestionsCategory: ServerThere are the same query conditions in SQL statements, but different fields need to be queried. Can there be a simple way to write them?
Unique asked 3 weeks ago
select  *,
          ISNULL((ShLrbl*100),0) as AddShLrbl,
          IsNULL((select [Name] from [3d_User] where [3d_User].Id=[3d_De_Cls_Record].ShrId),'') as ShrName,
          ISNULL((select State from [3d_ Customer_ MinLrBL] where De_ Id=(select distinct ParentId from [3d_ De_ Cls_ Record] where Pid=22) and ClsId=(select distinct ClsId from [3d_ De_ Cls_ Record] where PID = 22), 0) as lrblstate, -- the two conditions are the same and the query fields are different
          ISNULL((select Name from [3d_User] where Id=ShLrblId),'') as ShLrblName,
          ISNULL((select Lr from [3d_ Customer_ MinLrBL] where De_ Id=(select distinct ParentId from [3d_ De_ Cls_ Record] where Pid=22) and ClsId=(select distinct ClsId from [3d_ De_ Cls_ Record] where PID = 22), 0) as minlr -- the two conditions are the same and the query fields are different
from [3d_De_Cls_Record] order by State asc,Id desc
Unique replied 3 weeks ago

I write code format may be some problems, not very good-looking, please understand

2 Answers
owlCoder answered 3 weeks ago

Are you on a project or on your own?
If you write the code on the project, you are too lazy to read it
If it’s something you’re learning, look at the syntax of SQL server. Multi table join query using this to write will be much easier, you read after you write this SQL

Unique replied 3 weeks ago

I’m working, but I’m also studying, and I’ve just started. Multi table query, I’ll have a look

Unique replied 3 weeks ago

Look at the following table query, the results found that the teacher had said before=||

owlCoder replied 3 weeks ago

This is the foundation. That’s why I asked you if you are self-taught! You write so many sub queries, and the query consumes resources. Not to mention the poor readability, your later maintenance is enough for you

HBU_DAVID answered 3 weeks ago

Define a string, assign the same place to the string, call the string later, and then splice the different parts together.