Pitu
Pitu

Reputation: 159

MySQL Query from 3 tables

I have 3 tables.

Mark
id_mark
mark_name

example record: 1, 'Ford'


Model
id_model
id_mark
mondel_name

example record: 1, 1, 'Focus'


Adds
id_adds
id_model
name
price
etc.

example record: 1 1 'My ad', 20000

How to execute a query that returned result example:

My ad Ford Focus 20000

Upvotes: 0

Views: 155

Answers (3)

Inm0r74L
Inm0r74L

Reputation: 205

select a.name, ma.mark_name, mo.model_name, a.price
from adds a, mark ma, model mo
where a.id_model = mo.id_model
and ma.id_mark = mo.id_mark

Be carefull with the data model, if you query just like that and there is more than one mark for the model you gonna get something like

My ad Ford Focus 20000 My ad Fiat Focus 20000

Upvotes: 0

WWW
WWW

Reputation: 9860

Using your exact spellings:

SELECT a.name, mk.mark_name, mo.mondel_name, a.price
FROM Adds a
INNER JOIN Model mo ON mo.id_model = a.id_model
INNER JOIN Mark mk ON mk.id_mark = mo.id_mark

Though instead of including id_mark in your Model table, I think you should have a join table that consists of nothing but id_model and id_mark and the key is comprised of both fields.

Upvotes: 1

ceth
ceth

Reputation: 45295

select adds.id_model, mark.mark_name, model.model_name adds.price
from mark, model, adds
where model.id_mark = mark.id_mark and adds.id_model = model.id_model

Upvotes: 0

Related Questions