Simple to achieve remote login authentication - mysql dump function using IP information group_concat

Book hates few when used. Recently I encountered a function point: record IP information gathering statistics for each user to user whitelist entries from the log table.

This function is performed only once (initialization), but the amount of data may be larger, execute java code / python script is slow. Just as I was overwhelmed and my little partner consider complete with stored procedures, Fan total understatement to say, not that a SQL Well, doing so complicated?

Face a question mark, the simplest solution I envisage will need two nested for loop, a SQL can be solved?

Domingo told me that this time a function: group_concat. It is this function, and instantly solve my problem.

1. Demand

Want to implement a user whitelist feature to reduce the frequency of off-site verification,SMS costs reduce spending, Enhance the user login experience.

2. The principle

For similar functionality not previously had contact, this method is implemented: IP whitelist new users in the user table field for storing user IP landing after the first three; determines whether landing in a city. This approach is relatively stupid, some cities have a larger span of IP addresses. After the time to write a method to distinguish the exact provinces it. Bigwigs also welcomed the guidance, let me learn other authentication methods.

3. Data Import

Previously, the use of large tables to record call record of a user interface, wherein the user comprises a user ID and IP information. In this table, a user corresponding to multiple rows, one IP may also correspond to multiple records (corresponding to a plurality of accounts may be). I subconsciously wanted to write a java project or python script to complete the job. But the need to build Java tools java project to determine the dependence circumstances; python native database link processing is slow (large volumes of data). I Kobayashi and after discussions, think or write a stored procedure is relatively simple to solve tricky.

Just when ready hands, and I and Domingo complained the sentence: such a complex function, how could a complete SQL? . Domingo understand my needs, it indicates that a SQL enough to complete the clean up imported user data table, and I recommend a function: group_concat.

group_concat, And concatthe like, for a string of splicing. But the concatscenario is different group_concatmainly applied to the group byresult field after stitching classification statement. The basic syntax is:

group_concat( [distinct] 待连接字段 [order by 排序字段 asc/desc ] [separator '分隔符'] )

The inquiry into its statement selectafter the batch can be completed easily obtain IP information of the user group.

select SELECT create_user_id, group_concat( DISTINCT concat( '#', SUBSTRING_INDEX   ( ip_address, '.', 3 ) ) SEPARATOR '' ) FROM sys_log GROUP BY create_user_id;

In order to meet design (IP whitelist format # xxx.xxx.xxx): using a third function to obtain SUBSTRING_INDEX string before; concat using the '#' splice IP field prior to the cut; '.' use keywords sEPARATOR group_concat function will default comma ( ',') replaced with a null separator. After the execution, only the user's IP whitelist the user ID and the specified format.

Modifications user table can be directly placed in the above-mentioned update sql statement, increase the number of judges, SQL is a complete dump IP address:

update sys_user a
set a.address_ip = (SELECT group_concat(distinct concat('#', SUBSTRING_INDEX(ip_address, '.', 3)) separator '')
                    FROM sys_log b
                    where b.create_user_id = a.id
                    group by create_user_id);

END~

References:

Chapter 12 Functions and Operators-Mysql official documents

Sense Xie Mingge, Kobayashi proposed aid

Feel free to contact me to learn to discuss ~

mail: [email protected]
micro-channel: Hello-wgh0807
QQ: 490 536 401

Guess you like

Origin www.cnblogs.com/wgh0807/p/11654659.html