1

I just began to use SQL joins. It's brand new for me. I tried INNER JOIN. It works but i need something more complex.

I have 3 tables:

  1. Topics - i keep here list of articles categories. (id, title)

  2. Content - i keep here list of the articles (id, title, topic_id, text, cpc, user_id)

  3. Users - here is list of users (id, username, coins)

What i want to do is: List all the topics first (showing the title), and right after each title show 4 of content records (with the same topic_id as current topics.id) and order records by this algoryth:

  1. Select 4 records From content which authors (content.user_id) have enought coins (in users table). I can do it in a while loop with a simple condition in php and check if content.cpc < users.coins if it is, don't echo this record. It works fine only if those 4 records have different 4 authors (user_id). But, If at least 2 of those records have the same author (user_id) then result = content.cpc[1] + content.cpc[2] (compare result with users.coins and if result is less, show just 1 of those 2 records) i mean user_id have enought coins just for 1 record tot be displayed.

  2. -> From (1) ORDER BY content.cpc DESC

  3. -> From (2), ORDER BY content.date DESC

What my brain is able to do at this moment is:

$rs3 = $connector->query("SELECT * 
FROM 
(SELECT content.*, users.coins 
  FROM content 
  INNER JOIN users ON content.user_id=users.id 
  ORDER BY content.cpc DESC LIMIT 4
) a
ORDER BY a.date DESC");

It works but only if records have different user_id Hope you understood me, and thank you kindly for attention when reading that. If i should edit something just tell me.

erik258
  • 14,701
  • 2
  • 25
  • 31
Valeriu Mazare
  • 323
  • 5
  • 12
  • it's almost what i want but as i said `If at least 2 of those records have the same author (user_id) then result = content.cpc[1] + content.cpc[2] (compare result with users.coins and if result is less, show just 1 of those 2 records) i mean user_id have enought coins just for 1 record tot be displayed.` – Valeriu Mazare Mar 22 '14 at 23:44

0 Answers0