Select accessing two tables

Asked

Viewed 45 times

1

I have a Product table, with the fields Marker, model and type.

A second table referring to laptops has its fields related to price, RAM, other fields and the model, which is the same as the Product table.

How to make a select that tells me the marketing of a product by accessing its model code?

  • 1

    Hi @William, you should use the Inner Join to join the tables, in this link has some information.

1 answer

1


How about this?

SELECT p.marker
FROM produto p
INNER JOIN laptops lap ON p.model = lap.model
WHERE lap.model = ?

Where ? is the laptop model.

However, you might want to show various product and laptop fields:

SELECT p.marker, p.type, p.model, lap.preco, lap.ram, lap.outrocampo
FROM produto p
INNER JOIN laptops lap ON p.model = lap.model
WHERE lap.model = ?

However, if on the other hand you just want the model and nothing else, then considering that the query does not bring any field of the table laptops and that the code given to the laptop is the same as the table produto, soon you could simplify to have it:

SELECT p.marker
FROM produto p
WHERE p.model = ?
  • Thank you Victor, the two solutions worked, I ended up testing as follows: select laptop.model,speed,hd,Marker from laptop Inner Join product on (laptop.model = product.model)

  • @William If everything went well, then I ask you to please signal my answer as accepted (it is the green letter below the voting numbers).

  • @William Thank you. :D

Browser other questions tagged

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