简体   繁体   中英

Find all rows not matching a criteria, MySQLi MySQL PHP

Alright, I have never asked a question here before, but here goes.

In my PHP script I currently have a query that retrieves all threads matching user ID in the threads_read table (records all read thread ids, Unix timestamp, and user ID). Then, I use another query to retrieve all the matching rows in an important_threads table that contains thread id.

This may be a very simple question to answer but I'm not too familiar with MySQL(i), but how would I make a query that retrieves all NON matching rows in an important_threads table?

You could do something like:

SELECT * FROM threads_read WHERE id NOT IN (SELECT id FROM important_threads)

That is a little resource heavy, but it does the job.

I would really recommend against that type of a database structure, though. Simply marking a table called 'threads' that would have a column to see if it was read or important would be a much more fluent design.

Best of luck!

You could do a LEFT JOIN on the important_threads table and only get the rows where there are no matches in the table:

SELECT 
    a.*
FROM 
    threads_read a
LEFT JOIN
    important_threads b ON a.id = b.id
WHERE 
    a.user_id = 14 
    AND 
    b.id IS NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM