I have two tables.
Table 1.
========
id
post
--------
Table 2.
========
id
post_id
thumbs_sum
--------
Query looks like:
SELECT DISTINCT(t2.id), t2.sum, t1.id
FROM table1 t
LEFT JOIN table2 t2 ON t2.post_id = t1.id
ORDER BY t2.sum DESC
For example, we have 5 posts (sum – sum of thumbs up and down):
1. sum = 3
2. sum = 1
3. sum = 5
4. sum = null
5. sum = -2
Post number 4 hasn’t any record in Table 2, that’s why my query return the next:
1. sum = 5
2. sum = 3
3. sum = 1
4. sum = -2
5. sum = null
How to decide this problem if I haven’t ability to change the structure of database tables and to sort result in PHP?
I’m trying to figure out what is the actual question here. “How to decide this problem” is a little abstruse.
Anyway, I assume, the problem here is we have
nullvalues as thumbs sum for posts that have no votes. When we order them, we’ll probably want to replace nulls with zeros.