user4308245
user4308245

Reputation:

mysql - Select with "if" preference

I need to load 50 entires with preference. let's assume i have

User_id  mid     preference
564      djng    2
123      e3r3    1
423      u5h5    1
544      djnw    2
545      njdw    2

The thing is to load firstly the entires with perference "1" and if there is no "1" then load all other left, with limit 50, so when we have 40 entires with preference "1" and for instance 100 with preference "2" so mysql query gets first 40 entires with preference "1" and then append left 10 with preference "2"

Currently i have

  $query = "SELECT * FROM $db1 WHERE userid!='$userid' AND mediaid NOT IN 
           (SELECT mid FROM $db2 WHERE uid='$userid')";

Upvotes: 1

Views: 133

Answers (2)

what
what

Reputation: 1

$query = "SELECT * FROM $db1 WHERE userid!='$userid' AND mediaid NOT IN (SELECT mid FROM $db2 WHERE uid='$userid') ORDER BY preference ASC LIMIT 50";

Upvotes: 0

xlecoustillier
xlecoustillier

Reputation: 16351

Your query seems to have little to do with what you're asking, but the general way of doing this is to use ORDER BY and LIMIT:

SELECT ...
FROM ...
WHERE ...
ORDER BY preference
LIMIT 0, 50

Upvotes: 1

Related Questions