Select 2 Rows from Table when COUNT of another table

Posted by Marcus on Stack Overflow See other posts from Stack Overflow or by Marcus
Published on 2010-06-14T20:51:15Z Indexed on 2010/06/14 21:42 UTC
Read the original article Hit count: 223

Filed under:
|
|
|

Here is the code that I currently have:

   SELECT `A`.* 
     FROM `A`
LEFT JOIN `B` ON `A`.`A_id` = `B`.`value_1` 
    WHERE `B`.`value_2` IS NULL 
      AND `B`.`userid` IS NULL
 ORDER BY RAND() LIMIT 2

What it currently is supposed to do is select 2 rows from A when the 2 rows A_id being selected are not in value_1 or value_2 in B. And the rows in B are specific to individual users with userid.

What I need to do is make it also so that also checks if there are already N rows in B matching a A_id (either in value_1, or value_2) and userid, and if there are more than N rows, it doesn't select the A row.

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql