Home > other >  How to make use of GORM in data query and get the total number of rows.
How to make use of GORM in data query and get the total number of rows.

Time:09-16

I now use the following method, using the data query, gorm in use, please limit () under the premise of how to get the total number of rows of data to check? Such as sql_calc_found_rows?
 
Webdb. Select (queryDBInfo. QueryInfos). The Where (queryDBInfo. QueryCriteria). The Order (queryDBInfo. SortCriteria). Offset (queryDBInfo. Offset), Limit (queryDBInfo. Count). Find (& amp; Find)

CodePudding user response:

Can be like this: the Table (Table name). The Where (conditions). The Count (& amp; TotalSize). Sort (Sort). Offset (Offset). Limit (Limit). Find (values)
Principle is to first according to the conditions of the count, and then query paging, close test, effective!

CodePudding user response:

Don't recommend this

Two queries, twice the possibility of error, the error to separate processing, don't be lazy
` ` ` go
Var count int
If e:=db. Model (& amp; Model). The Where (" x & gt; ?" The model), X), Count (& amp; The count). The Error; e! Nil={
Handle (e)
}
If the count!=0 {
If e:=db. Model (& amp; Model). The Where (" x & gt; ?" The model), X), Find (& amp; Models). The Error; e! Nil={
Handle (e)
}
}

CodePudding user response:

For example
Tx:=db. The Where (XXX). The Find (& amp; Data)
If the tx. Error==nil {
Tx=tx. Count (& amp; The count)
}

CodePudding user response:

Call first. The count (& amp; A) and limit (). Offset (). The Scan (& amp; b)

CodePudding user response:

The
reference 3 floor qybao response:
for example
Tx:=db. The Where (XXX). The Find (& amp; Data)
If the tx. Error==nil {
Tx=tx. Count (& amp; The count)
}


refer to the second floor fwhezfwhez response:
don't recommend this

Two queries, twice the possibility of error, the error to separate processing, don't be lazy
` ` ` go
Var count int
If e:=db. Model (& amp; Model). The Where (" x & gt; ?" The model), X), Count (& amp; The count). The Error; e! Nil={
Handle (e)
}
If the count!=0 {
If e:=db. Model (& amp; Model). The Where (" x & gt; ?" The model), X), Find (& amp; Models). The Error; e! Nil={
Handle (e)
}
}


Upstairs said "query twice, there are two errors may" to separate check? Don't understand why want to separate! Check any error in the error count and the data can be according to the business to consider whether to suspend the process, personal feel no sense must separate, unless it is business process needs!

CodePudding user response:

Because gorm find and count is separately issuing SQL, you can print the SQL log, will issue two SQL, a select *, a select count (*), because it is two SQL, so it could be a success, a failure, or two failed, if the first fails, it is not necessary to carry out the second, make a judgment is reasonable, so separated

CodePudding user response:

refer to 6th floor qybao response:
because gorm find and count is separately issuing SQL, you can print the SQL log, will issue two SQL, a select *, a select count (*), because it is two SQL, so it could be a success, a failure, or two failed, if the first fails, it is not necessary to carry out the second, make a judgment is reasonable, so separate

Behind that I added a point, the count of the select with the count (*), whether or what is the count (*) or other, I'm not sure; If the where condition correctly, this is not a problem, but if there is no separate, and written in the select column is likely to go wrong, so need not be successful at the same time,


Above, I still feel that if this problem can be tested, then talk about this any eggs? So in the end, I think together is no problem,

CodePudding user response:

refer to 6th floor qybao response:
because gorm find and count is separately issuing SQL, you can print the SQL log, will issue two SQL, a select *, a select count (*), because it is two SQL, so it could be a success, a failure, or two failed, if the first fails, it is not necessary to carry out the second, make a judgment is reasonable, so separate

If the where condition correctly, this together is not a problem

CodePudding user response:

reference Nihility/reply on the eighth floor:
Quote: refer to the sixth floor qybao response:
because gorm find and count is separately issuing SQL, you can print the SQL log, will issue two SQL, a select *, a select count (*), because it is two SQL, so it could be a success, a failure, or two failed, if the first fails, it is not necessary to carry out the second, make a judgment is reasonable, so separate

If cases, right in the where clause for the sentences together to write is not a problem of
that can only say you think too simple, the use case issued two SQL, it is destined to have a time difference (even to a multithreaded execution time difference), if subjective cognizance time difference will not affect you, that you still so hard to find a way to solve the problem of transaction and concurrency dry hair?

CodePudding user response:

references 9 f qybao response:
Quote: refer to the eighth floor of the Nihility/reply:
Quote: refer to the sixth floor qybao response:
because gorm find and count is separately issuing SQL, you can print the SQL log, will issue two SQL, a select *, a select count (*), because it is two SQL, so it could be a success, a failure, or two failed, if the first fails, it is not necessary to carry out the second, make a judgment is reasonable, so separate

If cases, right in the where clause for the sentences together to write is not a problem of
that can only say you think too simple, the use case issued two SQL, it is destined to have a time difference (even to a multithreaded execution time difference), if subjective cognizance time difference will not affect you, that you still so hard to find a way to solve the problem of transaction and concurrency dry hair?


I have said, in front of the business need to open it is no problem, just set out from the statement, I don't think I said no problem, but you want to extend the said concurrent cases, as you said that only two hair, how you can ensure that the results of the first and the second is the same? Don't select can lock? Please advise, thanks a lot!

CodePudding user response:

Pure from statement is also a problem, mainly for you the where statement right here together will not be extended, a problem of SQL statement correct normal execution, do not represent SQL database state in change at any time, you can absolutely guarantee every SQL success? Because of this, to considering the robustness of the program, two SQL, respectively, the purpose is to prevent the first failure, it is not necessary to carry out the second, according to the thinking of you, if the first select fails, the second select count has succeeded (together, the end result is successful), you are to return the result or not return? Return result will cause the total number of, and detailed list is empty, it is not bring hidden trouble to the program bugs?
If the business data need to be completely consistent, the select lock and why not, lock line lock table can be even, the realization of the lock more,

CodePudding user response:

Not necessary, I can directly in the underlying processing error no longer distributed upwards
  • Related