M. Niyazi Alpay
M. Niyazi Alpay
M. Niyazi Alpay

I've been interested in computer systems since a very young age, and I've been programming since 2005. I have knowledge in PHP, MySQL, Python, MongoDB, and Linux.

 

about.me/Cryptograph

  • admin@niyazi.org

Pulling Information by Associating Tables in Database (Inner Join)

Pulling Information by Associating Tables in Database (Inner Join)

To fetch data from the database, we use the SELECT command. With a command like SELECT * FROM table_name, we retrieve our data from the table_name table. If we need to fetch data based on a condition, we specify our condition with WHERE.

SELECT * FROM blog WHERE id=1 retrieves the information with the id value of 1 from the blog table. There is also a category information for the topic with id value of 1 in the categories table, and also the information of the user who opened the topic is in the profiles table. Instead of executing separate queries for each of them, we can retrieve them all with a single SQL query.

In the Category table, there are fields named k_id and kat_isim, which hold the category name and id information.

In the Profile table, there are fields named p_id and kullanici_adi, which hold the user name and user's id information.

In the Blog table, there are fields named id, blog_baslik, blog_icerik, kat_id, kullanici_id, which hold the id information of the topic, content and title information, id information of the user who opened the topic, and the id information of the category it belongs to, respectively.

To fetch all of these in a single query, we use the following command:

SELECT * FROM blog b INNER JOIN profile p ON b.kullanici_id = p.p_id INNER JOIN blog_kategori bk ON b.kat_id=bk.k_id INNER JOIN social s ON p.p_id=s.user_id WHERE b.id=1

We selected the blog table and assigned this table name to a variable called b, then we did the same for the other tables. With b.kat_id=bk.k_id, we combined the kat_id field in the blog table with the k_id field in the blog_category table. This means that the information of the category whose id is the same as the category id stored in the blog table will be retrieved from the categories table. The same is true for the profile table. By connecting the kullanici_id field in the blog table with the id field in the profile table, the information is fetched. If we hadn't used this method, we would have had to execute separate queries for each, but this way, we performed more tasks with fewer queries.

Author

Cryptograph

You may also want to read these

There are none comment

Leave a comment

Your email address will not be published. Required fields are marked *