MySQL - Compare each field with the rest of the database
i'm getting crazy trying to figure this out with pure MySQL and no PHP.
I want to select rows on开发者_如何学Cly if the comparition of that row with the rest of the database gives certain number which is not important.
The thing im trying to achieve is something like this:
SELECT * FROM table WHERE field > ( SELECT f FROM table WHERE date < XXX )
I'm assuming that the query in brackets does not return a single value (because date < XXX indicates more values with respect to time). So...
I think what you need to try and do is form a relationship between table2 and table1 so that you can make a per-row field cell comparison.
E.g.
SELECT * FROM table1 t1
INNER JOIN (SELECT f FROM table2 WHERE data < xxx) t2
ON t1.foo = t2.bar
WHERE t1.field > t2.f
I suspect what you want is a combination of group, count, and having. For exampleif you wanted to find all users who have made more than 10 posts since a certain date, you could do
SELECT *, COUNT(posts.id) as pcount
from users
left join posts on (posts.user = users.id AND post_made > ZZZ)
HAVING COUNT(posts.id) > 10
The having clause lets you use aggregate fields like in a where clause.
精彩评论