Left JOIN right table multiple data duplicates

Source: Internet
Author: User


MySQL's left join queries the data from the two tables, with the left-hand table as the main and the right table as a supplement. If there are no contents in the right table in the left table, fill with null. This is the general common explanation. It's easy to understand. But when you do most of the right table, you have to write the conditions.

I write a product-the product image of the paging process needs to find a picture can be. But in the left join with MySQL, left table is product, and the key is PID. The right table is the image primary key is imageID, and the foreign key is PID.
Query statement in

Select product.* from product left join image using (PID);
When output results are found, when a product data to multiple picture data, the product will appear duplicate. This is the trouble, query the network people have such a problem,

But all through step-by-step to solve, that is, the query finished product data, and then the cycle of the picture query and query 1 output.

I thought for a long time, using another method to solve, left join right table multiple data, according to the above I wrote the following SQL statement
Select Product.*,foraspcn.image from product left join (select Imagename,pid from image GROUP by PID) as FORASPCN

using (PID);
This query out is a product corresponding to a picture

That is, in the query right connected table, the query is 1 data, through groupby to data retrieval. Of course, such group by is not sure to find the one.

That's how I solved it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.