Home > Database > Mysql Tutorial > How to Limit Matches from a Joined Table to One in MySQL?

How to Limit Matches from a Joined Table to One in MySQL?

Linda Hamilton
Release: 2024-11-16 12:44:02
Original
649 people have browsed it

How to Limit Matches from a Joined Table to One in MySQL?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template