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

How to use group_distinct to get the total number of records in MySQL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

How to use group_distinct to get the total number of records in MySQL, for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

How to get the total number of records (rows) after MySQL_ uses group_distinct

Question: how to get the total number of records of query results after using group or distinct and so on? -- required, do not use subquery

Analysis:

If you don't have group,count (*), that's fine.

However, it is different when used. The statistical function is used for group.

At this time, how to get the total number of query result records is a problem.

Feasibility analysis:

Is there any solution to this problem? If it's impossible, don't do it, such as perpetual motion machine, three equal points and so on.

Query results in the terminal of:

Mysql > select tag_id from labels where 1 = 1 group by tag_id

+-+

| | tag_id |

+-+

| | 0 |

| | 1 |

| | 2 |

| | 3 |

| | 4 |

| | 6 |

| | 9 |

| | 11 |

| | 22 |

| | 54 |

+-+

10 rows in set (0.00 sec)

Finally, there was a number of 10 rows.

Now that the terminals are available, there should be a place to store them. (it is speculated that mysql will not check it again by himself, because he will have the results.)

Solution: use the found_rows function

Mysql > select found_rows ()

+-+

| | found_rows () |

+-+

| | 10 |

+-+

1 row in set (0.00 sec)

Possible questions:

In the case of multithreading, will there be a problem?

In Java, problems occur when multiple threads hold a connection together.

T1 query select tag_id from labels where 1 = 1 group by tag_id

T2 query something else

T1 query select found_rows ()

The result is the query result of T2.

In the case of a thread pool such as Hibernate, each thread holds the current connection until the thread ends.

In this way, the connection of each thread is different and there is no conflict.

However, you must ensure that there is no other query sql execution between the two statements.

Other:

ROW_COUNT () can see the number of rows affected by the last update operation.

= END====

This is the answer to the question about how to use group_distinct to obtain the total number of records in MySQL. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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

Database

Wechat

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

12
Report