MySQL JOIN with LIMIT on Joined Table for Unique Matches
In MySQL, performing a join operation between two tables can result in multiple matches from the joined table for each record in the primary table. This can be undesirable when you only need one specific match, such as the first record.
To limit the number of matches from the joined table to one, you can utilize the LIMIT subquery technique. Here's an example of how it's done:
SELECT
c.id,
c.title,
(SELECT p.id FROM products AS p WHERE c.id=p.category_id ORDER BY p.id LIMIT 1) AS product_id,
(SELECT p.title FROM products AS p WHERE c.id=p.category_id ORDER BY p.id LIMIT 1) AS product_title
FROM categories AS c;
The above is the detailed content of How to Limit Matches from a Joined Table to One in MySQL?. For more information, please follow other related articles on the PHP Chinese website!