preface

  • 【 Music Blog 】The backend is developed in KOA, so potholes encountered and resolved during development are recorded


One aspect

1. If the query condition is empty, the MySql database does not query the query condition

  • Recently the development encountered a very common condition query, this condition can not pass.
  • It is not a special SQL. XML file like mybatis, you need to write SQL text. In this case, you need to determine whether each condition is null, and later found a very useful SQL statement, very simple to solve the problem.
  • Under the normal condition of multiple conditions, I will judge the existence of each condition in turn at the beginning, and then write SQL text according to the judgment result, so the content of this method is very large and easy to make mistakes
  • Mysql > alter table mysql > alter table mysql > alter table mysql
  • Select * from tablewhere(Field = condition or condition =' ')Copy the code

  • Select * from table where field = condition; select * from table where field = condition; Select * from table where ‘ ‘=’ ‘; So this is the case where multiple conditions are satisfied and the condition is not an empty unknown query.
  • select * from qiniu_photo where username = '${req.username}'     
    and ( file_name = '${req.form.fileName}' or '${req.form.fileName}'=' ')Copy the code

  • So that you don’t have the huge, error-prone approach that you had before.

The final summary

Koa as js background, or quite good, welcome to pit to communicate with me (continuous update) ~

The original address

Juejin. Cn/post / 684490…

reference

If the query condition is null: blog.csdn.net/qq_39651858…