If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

WHERE IN MySQL request

Started by sahildd, Oct 01, 2022, 01:13 AM

Previous topic - Next topic

sahilddTopic starter

Hello.

With the use of IN, I am able to choose the necessary lines that have the logins I have selected in them, utilizing an array known as 'logins'.

$sqlLogin='';
foreach($logins as &$login){
    if($sqlLogin!='')$sqlLogin.=", ";
    $sqlLogin.="'".$login."'";
}
 $sql="SELECT * FROM members WHERE login IN (".$sqlLogin.") ";

However, selecting all entries that do not contain the chosen logins is not clear.
For instance, new rows have appeared in the table (someone else put them there), and a query is needed that will select them.

What is your recommendation on how this can be accomplished?
Also, is it possible to select new records using the auto-increment field (id) present in the table?
  •  


soconsult01

What about NOT IN?

I am uncertain of the criterion needed for a new entry. However, it can be safely assumed that a record with an id of 2 was created after a record with an id of 1. In this case, it is possible to select new records using the auto-increment field (id) present in the table.

This is my preferred and most reliable way of doing this, and I use the following code:

SELECT * FROM table ORDER BY id DESC LIMIT 10
This code outputs the last ten entries in the table.
  •  

Brijesh

To select all entries that do not contain the chosen logins, we can modify the SQL query to use the NOT IN operator. The NOT IN operator is used to exclude a set of values from the result set. In your specific case, you can use the following code:

$sql="SELECT * FROM members WHERE login NOT IN (".$sqlLogin.") ";

In this query, the $sqlLogin variable contains a comma-separated list of logins you want to exclude from the result set. When executed, this query will retrieve all entries in the "members" table that do not have logins specified in the $logins array.

Now, for selecting new records using the auto-increment field (id) present in the table, you can use the following query:

$sql="SELECT * FROM members WHERE id > <last_id>";

In this query, replace <last_id> with the ID of the last record you have retrieved. For example, if the last record you processed had an ID of 100, you would use:

$sql="SELECT * FROM members WHERE id > 100";

This query will return all new records with IDs greater than the specified ID, effectively allowing you to select new records based on the auto-increment ID field.

By using these SQL queries, you can effectively select entries that do not contain chosen logins and also retrieve new records using the auto-increment field (id) in the table. This approach can be very useful for tracking and processing new entries in the table.
  •  

ZoogAdopy

You can modify your SQL query to use the NOT IN clause instead of IN. This will allow you to retrieve all records that have logins not present in your $logins array. Your updated SQL query would look like this:

$sql="SELECT * FROM members WHERE login NOT IN (".$sqlLogin.")";
If you want to select new records based on the auto-increment id field, you can use a query that retrieves records with an id greater than the maximum id of the current entries you have. For example:

$sql="SELECT * FROM members WHERE id > ".$currentMaxId;
Critically speaking, relying solely on the NOT IN clause can lead to performance issues, especially with large datasets. It's often better to consider using a LEFT JOIN or EXCEPT for complex queries, as they can be more efficient. Also, if you're not handling SQL injection properly when building $sqlLogin, you're opening yourself up to security vulnerabilities.

On another note, if you think using NOT IN is a good practice, think again. It can lead to unexpected results if any of the values in the array are NULL. Always sanitize your inputs and consider using prepared statements to avoid pitfalls.

If you're not leveraging indexes effectively on the login or id fields, you're just asking for slow queries.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...