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

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...

HONEST MAGENTO 2 REVIEW FOR ALL ECOMMERCE MERCHANTS

Ecommerce has had a significant impact on the way businesses do business. Nowadays, you can do the majority of your business tasks with a few clicks of the mouse or a few taps on your mobile device. While there are many big players in e-commerce or e-business, Magento 2 has been the front runner in the online business world – all due to the number of good things it offers. However, at some point, the average person is going to hit a wall. In today’s article, we’re going to focus on compiling a list of Magento 2 reviews so that you can learn everything about the platform from its background to key features, pros, and cons and decide whether you’d want Magneto for your e-commerce or not. What is Magento 2 platform? For online retailers, Magento is the most popular e-commerce platform. It is a high-performance, scalable solution with robust out-of-the-box capabilities and a big community of developers who are constantly adding new features to the platform. Magento 2 is the compan...