Suppose I have a query like this:
SELECT t1.name, t2.likes
FROM table_1 t1
LEFT JOIN table_2 t2 ON t1.name = t2.name
How could that left join be modified to make it do the following:
SELECT likes FROM table_2 WHERE name = [name from table 1] ORDER BY likes ASC
I tried writing a subquery, but it didn’t work because (I assume) you can’t use a variable from outside the subquery (e.g. the name) inside the subquery (the name is not constant, as the result set from the query will be more than one).
Is that possible to do?
EDIT: The tables would be something like this:
table_1:
+------+--------+
| id | name |
+------+--------+
| 0 | cat |
| 1 | dog |
+------+--------+
table 2:
+------+--------+---------+
| id | name | likes |
+------+--------+---------+
| 0 | cat | 23 |
| 1 | cat | 2 |
| 2 | cat | 53 |
| 3 | dog | 25 |
| 4 | dog | 12 |
+------+--------+---------+
So, what I’m wanting is:
+--------+---------+
| name | likes |
+--------+---------+
| cat | 2 |
| dog | 12 |
+--------+---------+
You can do it with a subquery too but i think that will be slower: