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:
Topics
- i keep here list of articles categories. (id, title)Content
- i keep here list of the articles (id, title, topic_id, text, cpc, user_id)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:
Select 4 records From
content
which authors (content.user_id
) have enoughtcoins
(inusers
table). I can do it in a while loop with a simple condition in php and checkif content.cpc < users.coins
if it is, don'techo
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
) thenresult
= content.cpc[1] + content.cpc[2] (compareresult
withusers.coins
and ifresult
is less, show just 1 of those 2 records) i meanuser_id
have enought coins just for 1 record tot be displayed.-> From (1) ORDER BY content.cpc DESC
-> 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.