How to do Inner Join in mysql?

Asked

Viewed 5,284 times

2

I have 2 tables in my database, Categories and Cars. The table categories has a PK Id_category and Name. Already Automobiles has a PK Id_automoveis and the other columns that make up this table INCLUDING the FK Id_categoria as you can see in the image below: Tabela automoveisqui

I’m starting my studies in databases with mysql but I’m having a lot of difficulties in relational models and Inner Join. If there’s something wrong I can change it too!

This database is handled by a PHP system.

  • INNER JOIN is a heavy query, only used when you really want all the records of the other corresponding table.

  • 2

    We should not use functions of the "mysql" extension because its development has been discontinued.Since you are starting your studies I suggest starting with mysqli or PDO.

  • Related: https://answall.com/q/8319/64969

  • Related: https://answall.com/q/17522/64969

2 answers

4

Your database is not normalized. First, it would normalize the BD, thus creating the tables:

  • Brand;
  • Model;
  • Coloured;
  • Category;
  • Automobile;

After that, I would think about doing the queries, where you can use different joins:

INNER JOIN or JOIN only: Returns records that have values corresponding in both tables;

LEFT JOIN: returns all the records from the table on the left and the corresponding records from the right table;

RIGHT JOIN: return all records from the right table and the Corresponding records from the left table;

FULL (OUTER) JOIN: Returns all records when there is one match in left or right table

  • Yes, I did this first as a test. As I am not in a long time I decided to do the gambiara. It is not professional anything that will require 100% correction. But your idea was very good, I will take as a suggestion. Thank you very much!

4


Try it buddy:

select
    *
from
    categorias c
inner join
    automoveis a
on a.ID_Categoria = c.ID_categoria
  • 2

    Excellent, it worked, in case now just I list in my code. Now if I want to make an entry in the bank is just me register the category via id né.. good now with you from here! Thanks @Rick

Browser other questions tagged

You are not signed in. Login or sign up in order to post.