Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the problems with SQL's use of if in business

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the problem of SQL using if in business? many novices are not very clear about it. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

If (true,'1','0')

If is an either-or choice in sql, so you need to pay attention to business issues when using it:

For example:

Determine whether a user is newly registered or an old registered user

Select

Tb.*

, if (t_user.user_id is not null,' newly registered user', 'old registered user') as new_sign_user

From tb

Left join t_user # # user id is unique

On tb.ds = t_user.ds

And tb.user_id = t_user.user_id

There is no problem in registering new and old users, because users have only two attributes, either new registration or old registration, and there is no problem with using if

Problem: judging the select of new paying users

Tb.*

, case when t_first_pay.from_user_id is not null and t_first_pay.ds < tb.ds then 'old payment'

When t_first_pay.from_user_id is not null and t_first_pay.ds = tb.ds then 'new payment'

Else 'unpaid' end as new_pay_user

From tb

Left join t_trade_flow_first_type as t_first_pay # # first payment by users

On tb.ds = t_first_pay.ds

And tb.user_id = t_first_pay.user_id

Paying users have three attributes: old payment attribute, new payment attribute and non-payment attribute, so if decision can no longer be used. Multiple decisions need to be made using case when.

If works best, but you also need to pay attention to business scenarios and exhaust business possibilities.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report