I'm trying to check two tables to see if an IP exists in either. The code I am using works but the number of rows being returned shows the full tables are being searched. The tables can be huge so I would like to limit the checking. Below shows what I am trying to do. When I run it on the actual database, the result of the echo shows 61,012, which is the number of rows in table b. Would someone please explain how I can limit the result so each table has a limit of 1?
table a {
status tinyint DEFAULT '0' NOT NULL,
ip INT UNSIGNED NOT NULL
}
table b {
ip INT UNSIGNED NOT NULL
}
INSERT INTO b VALUES ('1.129.107.125');
$ip = '1.129.107.125';
$ck_query = mysqli_query("SELECT 1 FROM a WHERE status = 0 and ip = INET_ATON('" . $ip . "')
or (SELECT 1 FROM b WHERE ip = INET_ATON('" . $ip . "') limit 1)");
echo 'row '.mysqli_num_rows($ck_query);
By the way, when I searched for ways to check the two tables I found a number of examples that said to use select exists. The problem is that that always returns true, unless I implemented it incorrectly, so I can't use it.