Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-02 00:41:16 09月20日 Update

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.

Tags: User question business attribute either help clear either or content only possibility uniqueness scenario this article novice more best knowledge industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux Redmi Xiaomi macOS