Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to implement this complex query using SQL SELECT statements?

To prevent bulk registration (the site uses a WordPress program), I installed a "Register IPs" plugin to record user registration IPs. This plugin adds additional data to a new user’s wp_usermeta data under the signup_ip key.

enter image description here

I don’t know much about SQL SELECT statement, is it possible to use SQL SELECT statement to get all registered IPs and sort them by the number of IP appearances? For example, query the 10 most registered IPs, so that I can find suspicious IPs and block them in time.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is my current attempt and I don’t know how to sort by IP occurrences. Any help, thanks in advance!

SELECT * 
FROM wp_usermeta 
WHERE meta_key = 'signup_ip' 
ORDER BY meta_value ASC 
LIMIT 0, 10;

>Solution :

You may aggregate by the user_id and then pivot out the IP address for use in sorting.

SELECT ip, COUNT(*) AS cnt
FROM
(
    SELECT MAX(CASE WHEN meta_key = 'signup_ip' THEN meta_value END) AS ip
    FROM wp_usermeta
    GROUP BY user_id
) t
GROUP BY ip
ORDER BY cnt DESC
LIMIT 10;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading