I want to join two tables, but only get 1 record of table2 per record on table1
For example:
SELECT c.id, c.title, p.id AS product_id, p.title
FROM categories AS c
JOIN products AS p ON c.id = p.category_id
This would get me all records in products
, which is not what I want. I want 1 [the first] product per category (I have a sort
column in the products field).
How do I go about doing that?
What about this?
Assuming you want product with
MIN()
imial value insort
column, it would look something like this.I like more another approach described in a similar question: https://stackoverflow.com/a/11885521/2215679
This approach is better especially in case if you need to show more than one field in SELECT. To avoid
Error Code: 1241. Operand should contain 1 column(s)
or double sub-select for each column.For your situation the Query should looks like: