Skip to main content

HOW TO JOIN TABLES PROGRAMMATICALLY IN MAGENTO 2

Today we talk about How to join tables programmatically in Magento 2. Sometimes you need to get join collection with product collection data or category collection data or order collection or custom table collection. In order to make you do that with ease, the developer team from Magenest recommends the topic join tables in Magento2. Here we go.

How to join tables in SQL?

A SQL Join statement is used to combine data or rows from two or more tables based on a common field between them. Different types of Joins are:

  • INNER JOIN: Returns records that have matching values in both tables.
  • LEFT JOIN: Returns all records from the left table, and the matched records from the right table.
  • RIGHT JOIN: Returns all records from the right table, and the matched records from the left table.
  • FULL JOIN: Returns all records from both tables. ( we don’t use this much)

For this sample, we have the following tables:

Director_idName
1Magenest director
2Magenest
3Son Tung
4Cris
5Magento2
Magenest_director

movie_idnamedescriptionratingdirector_id
1Harry PotterFantasy11
2PassengerAction22
3InsidiousHorror43
4PokemonAnime44
Magenest_movie

actor_idname
1Rowan
2Tung
3Satoshi
4Magenest
Magenest_actor
movie_idactor_id
11
22
23
33
44
41
Magenest_movie_actor

And their relationship:




 We want to get all records that have matching values from Magenest_movie, Magenest_director, and Magenest_actor. The query would be as follows:

SELECT `main_table`.name AS `movie`, `main_table`.description ,`main_table`.rating , `magenest_director`.`name` AS `director`, `magenest_actor`.name AS `actor` 

FROM `magenest_movie` AS `main_table` 

INNER JOIN `magenest_director` ON main_table.director_id=magenest_director.director_id 

INNER JOIN `magenest_movie_actor` ON main_table.movie_id=magenest_movie_actor.movie_id 

INNER JOIN `magenest_actor` ON magenest_actor.actor_id=magenest_movie_actor.actor_id;

Result:

moviedescriptionratingdirectorActor
Harry PotterFantasy1Magenest directorRowan
PassengerAction2MagenestTung
PassengerAction2MagenestSatoshi
InsidiousHorror4Son TungSatoshi
PokemonAnime4CrisMagenest
PokemonAnime4CrisRowan

Next, get the all director’s names and the matched movie’s names:

SELECT `main_table`.name as `movie`,`magenest_director`.name AS `director` 

FROM `magenest_movie` AS `main_table` 

RIGHT  JOIN `magenest_director` ON main_table.director_id=magenest_director.director_id 
moviedirector
Harry PotterMagenest director
PassengerMagenest
InsidiousSon Tung
PokemonCris
NullMagento2

By using LEFT JOIN in this case, the result is:

moviedirector
Harry PotterMagenest director
PassengerMagenest
InsidiousSon Tung
PokemonCris

Sourcehttps://magenest.com/en/how-to-join-tables-programmatically-in-magento2/

Comments

Popular posts from this blog

10+ BEST ECOMMERCE WEBSITE BUILDER FOR SMALL BUSINESS TO CHECK OUT

  Small businesses need to find an eCommerce website builder that is affordable and easy to use. In this blog post, we will recommend the best eCommerce website builder for small business. We will also provide a few tips on how to get started. So, if you are looking for a good eCommerce website builder, you have come to the right place! Keep reading for more information. How to find the best eCommerce website builder for small business Depending on your demands, the best option for you will vary. For example, the modest online shop offering handmade goods is going to have different needs than the seven-figure dropship business. Despite these variations, the following standards should always be taken into account when choosing a platform: Ease of use You might have little to no experience constructing websites if this is your first attempt at an eCommerce site. A site from scratch can be challenging to build if you have no experience with coding or web design. The best eCommerce web...

TOP 7 ECOMMERCE PLATFORMS TO CONSIDER IN 2023

The last few years have witnessed the rise of the eCommerce platform with tons of features and look that suit each business’s requirements for   eCommerce websites . An eCommerce platform makes it possible for businesses to reach customers in online channels, hence helping increase the market size of the businesses.  While traditional eCommerce software is costly, not scalable, difficult to work with, and time-consuming considering customization and integration with other systems, the eCommerce platform is a better solution for creating relevant, engaging, and personalized online experiences. Among hundreds of eCommerce websites, you have to pick one that can decide the future of your business. In this article, we’ll explore 7 of the best eCommerce platforms and also cover their features, pros, cons, and whom they’re recommended for most, so take a look below, you can have all the key details and see which is a perfect fit for your unique business needs. 7 Best eCommerce platf...

The Top 10 Successful Shopify Stores in Australia and the Valuable Lessons They Offer

  In recent years, the eCommerce landscape has seen remarkable growth, particularly in Australia, where online businesses have flourished. Among the various platforms empowering entrepreneurs, Shopify stands out as a popular choice for Australian retailers. With its user-friendly interface, customizable designs, and a plethora of features, Shopify facilitates the creation of compelling online stores, enabling seamless connections with customers. This article dives into some of Australia's top Shopify stores, spotlighting their exceptional customer experiences, unique selling propositions, and the lessons we can glean from their triumphs. Whether you're an aspiring business owner or an enthusiastic online shopper, these stores serve as inspirational examples, showcasing how to thrive in the competitive realm of Australian eCommerce. Let's explore these captivating success stories and uncover the strategies that underpin their achievements. Leading Shopify Stores in Australia...