Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL join not returning rows
    primarykey
    data
    text
    <p>I'm attempting to create an anti-bruteforcer for the login page on a website. Unfortunately, my query is not working as expected. I would like to test how many times an IP address has attempted to login, and also return the ID of the user for my next step in the login process. </p> <p>However, I'm having a problem with the query... for one thing, this would only return rows if it was the same user as they had been trying to login to before. I need it to be any user. Secondly, regardless of whether I use LEFT JOIN, RIGHT JOIN, INNER JOIN or JOIN, it will not return the user's ID unless there is a row for the user in <code>login_attempts</code>.</p> <pre><code>SELECT COUNT(`la`.`id`), `u`.`id` FROM `users` AS `u` LEFT JOIN `login_attempts` AS `la` ON `u`.`id` = `la`.`user_id` WHERE `u`.`username` = 'admin' AND `la`.`ip_address` = '127.0.0.1' AND `la`.`timestamp` &gt;= '1' </code></pre> <p>Here's the output from <code>DESC login_attempts</code></p> <pre><code>Field Type Null Key Default Extra id int(10) unsigned NO PRI NULL auto_increment user_id int(10) unsigned NO MUL NULL ip_address varchar(15) NO MUL NULL timestamp int(10) NO NULL </code></pre> <p>This query does the same thing, except does not even select the ID if there is a row in login_attempts corresponding to it:</p> <pre><code>SELECT COUNT(`la`.`id`), `u`.`id` FROM `users` AS `u` LEFT OUTER JOIN `login_attempts` AS `la` ON `u`.`id` = `la`.`user_id` AND `la`.`ip_address` = '127.0.0.1' AND `la`.`timestamp` &gt;= '1' WHERE `u`.`username` = 'admin' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload